我有一个名为“scriptNastran.py”的简单Python脚本,它通过子进程函数调用一个shell:
import subprocess
subprocess.call(['sh', './launchNastran.sh'])我的launchNastran.sh是:
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN='/users/develop/tmp/input'
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN文件TRAM和MODELE与shell和Python脚本位于同一个目录中。该目录可以在Shell:CHEMIN='/users/develop/tmp/input中看到。
但是,目录在Python中发生了变化,因此我想将Python中定义的arg_directory作为shell的一个参数传递,如下所示:
import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh'])对于python脚本和shell,如下所示:
$ scriptNastran.py arg_directory
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN= arg_directory
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN有人知道怎么做吗?
(谢谢你的帮助:)
发布于 2021-06-03 09:56:45
可以将目录作为参数传递:
import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh', arg_directory])然后在shell脚本中阅读它。
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN="$1"
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch "$CHEMIN"发布于 2021-06-03 10:04:04
launchNastran.py
def call_shell(CHEMIN_path):
import subprocess
ssh_command_string='''
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN={path}
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN
'''.format(path=CHEMIN_path). ## put the path you have in format
subprocess.Popen(ssh_command_string, shell=True)
call_shell('/users/develop/tmp/input') ## call the shell scripthttps://stackoverflow.com/questions/67819218
复制相似问题