2016-04-27 101 views
2

现在我有一个测试file.dat,我运行hexdump并将输出放入hexdump.dat文件中。在subprocess.Popen命令中使用变量

subprocess.Popen(['hexdump file.dat > hexdump.dat' ], shell=True) 

作为一个方面说明,我所看到的建议,不使用shell=True但我基本上得到错误OSError: [Errno 2] No such file or directory

所以,我想能够传递一个变量或一个数组,文件,而不是硬编码的“file.dat”。 “文件”可以是用户输入或从前面的子流程部分生成的数组/列表。

我已经尝试了用户输入的情况下:

from subprocess import Popen, PIPE, STDOUT 
files = raw_input('File Name: ')                         
p = subprocess.Popen(['hexdump files > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)           
out,err = p.communicate(input=files)   

而且具有:

p = subprocess.Popen(['hexdump', inputs, ' > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)           

感谢您的帮助,我知道我没有理解正确这里所需要的结构,使一些“handholdy “答案将不胜感激。

+0

你试过的是什么问题? – user312016

+0

Error Output:Traceback(last recent call last): 文件“scratchpadDecoder.py”,第47行,在 subprocess.Popen(['hexdump file.dat> hexdump.dat']) 文件“/ dsw/python -2.7.1/lib/python2.7/subprocess.py“,第672行,在__init__中 errread,errwrite) 文件”/dsw/python-2.7.1/lib/python2.7/subprocess.py“,行1202,在_execute_child raise child_exception – stackofpancakes

回答

1

你需要shell=True,因为否则它会寻找具有该名称的可执行文件。 shell=True讲述使用shell执行命令的方法,所以>和朋友成为你最初的目标(重定向)。

下面的代码,你贴:

from subprocess import Popen, PIPE, STDOUT 
files = raw_input('File Name: ')                         
p = subprocess.Popen(['hexdump files > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)           
out,err = p.communicate(input=files) 

不会起作用,因为你只是路过fileshexdump,如果名为files一个文件不存在,你会得到一个错误(和如果它确实存在,它仍然可能不是你想要的)

你需要的是建立你执行字符串:

file = "input.dat" 
p = subprocess.Popen("hexdump " + file + " > hexdump.dat", shell=True) 
+0

哇谢谢。我以为我曾尝试过“+”组合,而逗号之间没有区别,但我想这是针对不同的问题。很高兴。 – stackofpancakes

+1

我也想指出,当我回答你关于变量的问题时,我发布的代码并不是真正的习惯用法。不使用shell = True并使用子进程模块提供的重定向的答案有更好的代码。 – itdoesntwork

-1

首先,关于未找到文件,您可能需要指定当前工作目录。

subprocess.Popen(['hexdump file.dat > hexdump.dat' ], shell=True, cwd='/bar/foo') 

在问候传递一个数组作为参数,也就是通常是这样的:

args = [ 'hexdump', ] + inputs 
subprocess.Popen(args, cwd='/foo/bar') 
+0

我不需要像这样:args = ['hexdump',] + inputs + [> hexdump.dat]? – stackofpancakes

1

而是与>重定向的,您可以使用标准输出重定向PARAM。至于文件的列表,你可以文件的列表追加到一个数组,包括hexdump都可以,即

myfiles = ['file1','file2'] 
with open('hexdump.dat', 'w') as output: 
    proc = subprocess.Popen(['hexdump'] + myfiles, stdout=output) 
+0

唯一的问题是我认为它把所有的hexdump放到一个hexdump.dat中,我仍然需要将它们分开以备后用。但是,当我解决阵列问题时,我会看到有关这方面的内容。 – stackofpancakes

+0

数组问题是什么?如果你想输出多个文件,你需要使用一个循环或类似的东西 – wonton

+0

不幸的是,这会导致错误:TypeError:只能连接列表(不是“str”)列出 – stackofpancakes

3

Warning: Passing shell=True can be a security hazard if combined with untrusted input. See the warning under Frequently Used Arguments for details.

喜欢的东西:

with open('hexdump.dat', 'wb') as f: 
    p = subprocess.Popen(['hexdump', 'file.dat'], stdout=f) 
    p.wait() 

您应该Popen念起来的shell参数做什么,并做出决定。

+0

你可以使用'subprocess.check_call()'而不是'Popen()。wait()'这里 – jfs

+0

是的,我只是保留了主题。 – totoro

0

我发现我做shell重定向与Python和变量如下:最简单的方法:

subprocess.check_output('svnadmin load %s < %s' % (repo, fname), shell=True) 

它可以处理非常大的文件。