python - 从 python 执行一个 .sh 文件,指示绝对路径

标签 python linux bash shell

我是 python 世界的新手,我在运行 bash 文件时遇到了一些问题,该文件将自动从我的 python 脚本(使用 linux)中运行。

我将我的 python 脚本设置为在我桌面的某个目录中创建一个文本文件 .geo 和一个 Bash 文件 .sh,如下所示:

    basedirectory="/home/pst2/Desktop/";
    *//Writing the .geo file* 
    file = open(basedirectory+nomdossier+"/"+nomfichier+".geo", 'w');
    file.write
        ..blabla
        ..blabla
    file.close();
    //Writing the .sh file 
    file = open(basedirectory+nomdossier+"/"+nomfichier+".sh", 'w');
    file.write
        ..blabla
        ..blabla
    file.close();

现在,我的脚本可以完美地处理所有设置的变量并正常工作,我创建的两个文件都位于该目录中(例如,在运行 python 脚本并输入变量后)

     /home/pst2/Desktop/test/ 
     (and in here you will find the new test.geo and test.sh that were created via the python script)

基本上,当使用 Bash test.sh(每当我在 ubuntu 上的目录中)“手动”执行时,test.sh 将在同一目录中创建另一个名为 test.msh 的文件 而且我似乎无法找到正确的编码,使用子进程模块从脚本自动执行新创建的 test.sh 文件。 有没有办法做到这一点,比如指明 .sh 文件的绝对路径 (在我们的例子中是 basedirectory+nomdossier+"/"+nomfichier+".sh )?

最佳答案

看看 os 模块。

我相信

os.system("command_line_with_args") 

可能是你要找的

关于python - 从 python 执行一个 .sh 文件,指示绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41090277/

相关文章:

python - 如何在 Keras 中保存经过训练的模型以便在应用程序中使用它?

android - android内核编译错误

bash - 为什么猫的大小比实际文件小?

linux - mosquitto-client 获取拒绝连接

Python - 使用 K-means 聚类。一些方差为零的列

python - 为什么 np.var(x) 和 np.cov(x, y) 给我不同的值?

python - 如何仅使用 .pyo 文件(没有 .py)来使用 mod_wsgi?

python - 如何在 Linux 上安装 OpenAi 的 Gym Atari 依赖项?

php - Apache httpd 在 Linux 服务器上挂起

Bash seq 在for循环中产生两个独立的数字序列