使用来自 python 的可变参数调用 linux 命令
call linux command with variable argument from python
我正在尝试从 python2x.
执行一个程序
在终端中,作业将 运行 为:
mpirun -np 8 ~/WORK/scf Fe_SCF.inp > Fe_SCF.out
其中Fe_SCF.*
是输入输出在CWD
.
现在,我正在尝试从 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 正在使用 "Real" 文件名,但不适用于变量名:
os.system("mpirun -np 8 ~/WORK/scf scfin" )
那么,使用这两种方法中的任何一种,我怎样才能用变量名作为输入和输出来调用程序?
在后一个使用 OS 模块的示例中,您应该能够做到:
os.system("mpirun -np 8 ~/WORK/scf "+ var_name)
给运行你的函数调用。
对于多个变量,d o:
os.system("mpirun -np 8 ~WORK/scf " + var_1 + " " + var_2)
调用需要一个列表,因此您的第一个示例应该是:
cmd = ['/absolute/path/to/mpirun', '-np', '8', '~WORK/scf', var_1]
call(cmd, stdout=var_2, stderr=STDOUT)
我正在尝试从 python2x.
执行一个程序在终端中,作业将 运行 为:
mpirun -np 8 ~/WORK/scf Fe_SCF.inp > Fe_SCF.out
其中Fe_SCF.*
是输入输出在CWD
.
现在,我正在尝试从 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 正在使用 "Real" 文件名,但不适用于变量名:
os.system("mpirun -np 8 ~/WORK/scf scfin" )
那么,使用这两种方法中的任何一种,我怎样才能用变量名作为输入和输出来调用程序?
在后一个使用 OS 模块的示例中,您应该能够做到:
os.system("mpirun -np 8 ~/WORK/scf "+ var_name)
给运行你的函数调用。
对于多个变量,d o:
os.system("mpirun -np 8 ~WORK/scf " + var_1 + " " + var_2)
调用需要一个列表,因此您的第一个示例应该是:
cmd = ['/absolute/path/to/mpirun', '-np', '8', '~WORK/scf', var_1]
call(cmd, stdout=var_2, stderr=STDOUT)