使用python中的变量参数调用linux命令
我正在尝试从python2x执行一个程序 在终端中,作业将按以下方式运行:使用python中的变量参数调用linux命令,python,command-line,command,Python,Command Line,Command,我正在尝试从python2x执行一个程序 在终端中,作业将按以下方式运行: mpirun -np 8 ~/WORK/scf Fe_SCF.inp > Fe_SCF.out 其中Fe\u SCF.*是CWD中的输入和输出 现在,我尝试从python脚本运行这一部分。因为,我将它们定义为变量,并尝试调用为: call(["mpirun -np 8 ~/WORK/scf", scfin, scfout]) 给出错误: File "./triolith.py", line 38, in
mpirun -np 8 ~/WORK/scf Fe_SCF.inp > Fe_SCF.out
其中Fe\u 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
这会产生错误:
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("mpirun -np 8 ~/WORK/scf "+ var_name)
运行函数调用
对于多变量,d o:
os.system("mpirun -np 8 ~WORK/scf " + var_1 + " " + var_2)
在后一个使用OS模块的示例中,您应该能够:
os.system("mpirun -np 8 ~/WORK/scf "+ var_name)
运行函数调用
对于多变量,d o:
os.system("mpirun -np 8 ~WORK/scf " + var_1 + " " + var_2)
在后一个使用OS模块的示例中,您应该能够:
os.system("mpirun -np 8 ~/WORK/scf "+ var_name)
运行函数调用
对于多变量,d o:
os.system("mpirun -np 8 ~WORK/scf " + var_1 + " " + var_2)
在后一个使用OS模块的示例中,您应该能够:
os.system("mpirun -np 8 ~/WORK/scf "+ var_name)
运行函数调用
对于多变量,d o:
os.system("mpirun -np 8 ~WORK/scf " + var_1 + " " + var_2)
call需要一个列表,因此您的第一个示例应该是:
cmd = ['/absolute/path/to/mpirun', '-np', '8', '~WORK/scf', var_1]
call(cmd, stdout=var_2, stderr=STDOUT)
call需要一个列表,因此您的第一个示例应该是:
cmd = ['/absolute/path/to/mpirun', '-np', '8', '~WORK/scf', var_1]
call(cmd, stdout=var_2, stderr=STDOUT)
call需要一个列表,因此您的第一个示例应该是:
cmd = ['/absolute/path/to/mpirun', '-np', '8', '~WORK/scf', var_1]
call(cmd, stdout=var_2, stderr=STDOUT)
call需要一个列表,因此您的第一个示例应该是:
cmd = ['/absolute/path/to/mpirun', '-np', '8', '~WORK/scf', var_1]
call(cmd, stdout=var_2, stderr=STDOUT)
嗨…谢谢你的回复。它工作正常,除了var_2应该是写入我的输出的文件这一点。但这是将输出写入标准输出。我也试过
cmd=[…,var_1,“>”,var_2]
这也不起作用。hd1,这给我带来了错误SyntaxError:atstdout=var_2
hd1的无效语法,是的…非常好,谢谢你的回复。它工作正常,除了var_2应该是写入我的输出的文件这一点。但这是将输出写入标准输出。我也试过cmd=[…,var_1,“>”,var_2]
这也不起作用。hd1,这给我带来了错误SyntaxError:atstdout=var_2
hd1的无效语法,是的…非常好,谢谢你的回复。它工作正常,除了var_2应该是写入我的输出的文件这一点。但这是将输出写入标准输出。我也试过cmd=[…,var_1,“>”,var_2]
这也不起作用。hd1,这给我带来了错误SyntaxError:atstdout=var_2
hd1的无效语法,是的…非常好,谢谢你的回复。它工作正常,除了var_2应该是写入我的输出的文件这一点。但这是将输出写入标准输出。我还尝试了cmd=[…,var_1,“>”,var_2]
这也不起作用。hd1,这给了我错误SyntaxError:atstdout=var_2
hd1的无效语法,是的……非常好