文件已创建,但子进程调用说没有这样的文件或目录
File gets created, but subprocess call says no such file or directory
我正在尝试将子进程调用的输出重定向到文件
def get_arch():
global ip_vm
in_cmd = "uname -p"
with open('/home/thejdeep/arch_list',"w") as outfile:
print outfile
subprocess.call(in_cmd,stdout=outfile)
print "Hello"
for i in ip_vm:
cmd = "ssh thejdeep@"+i+" 'uname -p'"
with open('/home/thejdeep/arch_list',"a") as outpfile:
subprocess.call(cmd,stdout=outpfile)
文件已创建。我通过打印 outfile 了解这一点。但是,子进程调用 returns 一个 Errno[2] 没有那个文件或目录
您 subprocess.call
的第一个参数不正确。它应该是一个列表,而不是一个字符串。比较:
>>> subprocess.call('echo hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
有:
>>> subprocess.call(['echo', 'hello'])
hello
0
如果你真的想传递一个shell命令给subprocess.call
,你需要设置shell=True
:
>>> subprocess.call('echo hello', shell=True)
hello
0
您通常最好使用列表版本,因为这样您就不必担心命令字符串中 shell 个元字符的意外影响。
我正在尝试将子进程调用的输出重定向到文件
def get_arch():
global ip_vm
in_cmd = "uname -p"
with open('/home/thejdeep/arch_list',"w") as outfile:
print outfile
subprocess.call(in_cmd,stdout=outfile)
print "Hello"
for i in ip_vm:
cmd = "ssh thejdeep@"+i+" 'uname -p'"
with open('/home/thejdeep/arch_list',"a") as outpfile:
subprocess.call(cmd,stdout=outpfile)
文件已创建。我通过打印 outfile 了解这一点。但是,子进程调用 returns 一个 Errno[2] 没有那个文件或目录
您 subprocess.call
的第一个参数不正确。它应该是一个列表,而不是一个字符串。比较:
>>> subprocess.call('echo hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
有:
>>> subprocess.call(['echo', 'hello'])
hello
0
如果你真的想传递一个shell命令给subprocess.call
,你需要设置shell=True
:
>>> subprocess.call('echo hello', shell=True)
hello
0
您通常最好使用列表版本,因为这样您就不必担心命令字符串中 shell 个元字符的意外影响。