[英]call linux command with variable argument from python
我正在尝试从python2x执行程序。
在终端中,作业将运行为:
mpirun -np 8 ~/WORK/scf Fe_SCF.inp > Fe_SCF.out
在CWD
中输入和输出Fe_SCF.*
。
现在,我正在尝试从python脚本运行这段代码。 从那以后,我将它们定义为变量,并尝试调用为:
call(["mpirun -np 8 ~/WORK/scf", scfin, scfout])
给出错误:
File "./triolith.py", line 38, in <module>
call(["mpirun -np 8 ~/WORK/scf", scfin, scfout])
File "/usr/lib64/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
使用真实文件名也不能解决问题:
call(["mpirun -np 8 ~/WORK/scf", "Fe_SCF.inp", "Fe_SCF.out"])
这给出了错误:
File "./triolith.py", line 38, in <module>
call(["mpirun -np 8 ~/WORK/scf", "Fe_SCF.inp", "Fe_SCF.out"])
File "/usr/lib64/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我已经检查并可以确认,使用os.system正在使用“真实”文件名,但不能使用变量名,例如:
os.system("mpirun -np 8 ~/WORK/scf scfin" )
因此,使用两种方法中的任何一种,如何调用以变量名作为输入和输出的程序?
调用需要一个列表,因此第一个示例应为:
cmd = ['/absolute/path/to/mpirun', '-np', '8', '~WORK/scf', var_1]
call(cmd, stdout=var_2, stderr=STDOUT)
在使用OS模块的后一个示例中,您应该能够:
os.system("mpirun -np 8 ~/WORK/scf "+ var_name)
运行您的函数调用。
对于多个变量,请执行以下操作:
os.system("mpirun -np 8 ~WORK/scf " + var_1 + " " + var_2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.