[英]Using a variable in a subprocess.Popen command
Right now I have a test file.dat that I run hexdump on and put the output into a hexdump.dat file. 现在我有一个测试file.dat,我运行hexdump并将输出放入hexdump.dat文件。
subprocess.Popen(['hexdump file.dat > hexdump.dat' ], shell=True)
As a side note, I have seen suggestions to not use shell=True
but I essentially get the error OSError: [Errno 2] No such file or directory
. 作为旁注,我已经看到建议不使用shell=True
但我基本上得到错误OSError: [Errno 2] No such file or directory
。
So, I would like to be able to pass in a variable or an array, files, instead of the hardcoded "file.dat". 所以,我希望能够传入变量或数组,文件,而不是硬编码的“file.dat”。 "files" could be a user input or an array/list generated from a previous subprocess section. “files”可以是用户输入或从先前的子进程部分生成的数组/列表。
I have tried a user input case: 我试过一个用户输入案例:
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)
Also with: 还有:
p = subprocess.Popen(['hexdump', inputs, ' > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)
Thanks for the help, I know I'm not appropriately understanding the structure needed here so some "handholdy" answers would be appreciated. 感谢您的帮助,我知道我没有正确理解这里所需的结构,所以一些“手持”的答案将不胜感激。
Warning: Passing
shell=True
can be a security hazard if combined with untrusted input. 警告:如果与不受信任的输入结合使用,则传递shell=True
可能会造成安全隐患。 See the warning under Frequently Used Arguments for details. 有关详细信息,请参阅“ 常用参数”下的警告。
Something like: 就像是:
with open('hexdump.dat', 'wb') as f:
p = subprocess.Popen(['hexdump', 'file.dat'], stdout=f)
p.wait()
You should read up on Popen
and what the shell
argument does, and make your decision. 您应该阅读Popen
以及shell
参数的作用,并做出决定。
You need shell=True
because otherwise it'll look for an executable with that name. 你需要shell=True
,否则它会查找具有该名称的可执行文件。 shell=True
tells the method to use the shell to execute the command so >
and friends become what you originally intended them to be (redirection). shell=True
告诉方法使用shell来执行命令,以便>
和朋友成为你原本想要的(重定向)。
The following code you posted: 您发布了以下代码:
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)
will not work because you're just passing files
to hexdump
, and if a file with the name files
doesn't exist you'll get an error (and if it does exist, it's still probably not what you wanted.) 因为你只是将files
传递给hexdump
而无法工作,如果名称files
不存在,你将收到一个错误(如果它确实存在,它可能仍然不是你想要的。)
What you want is to build the string you're executing: 你想要的是构建你正在执行的字符串:
file = "input.dat"
p = subprocess.Popen("hexdump " + file + " > hexdump.dat", shell=True)
Instead of redirecting with >
, you can redirect using the stdout param. 您可以使用stdout参数重定向,而不是使用>
重定向。 As for the list of files, you can just append the list of files to an array containing hexdump, ie 至于文件列表,您只需将文件列表附加到包含hexdump的数组即ie
myfiles = ['file1','file2']
with open('hexdump.dat', 'w') as output:
proc = subprocess.Popen(['hexdump'] + myfiles, stdout=output)
I found the simplest way for me to do shell redirection with python and variables is as follows: 我找到了使用python进行shell重定向的最简单方法,变量如下:
subprocess.check_output('svnadmin load %s < %s' % (repo, fname), shell=True)
It can handle very large files. 它可以处理非常大的文件。
First off, in regards to file not found, you may need to specify the current working directory. 首先,关于找不到文件,您可能需要指定当前的工作目录。
subprocess.Popen(['hexdump file.dat > hexdump.dat' ], shell=True, cwd='/bar/foo')
In regards to passing in an array as an argument, that is typically something like: 关于传入一个数组作为参数,通常是这样的:
args = [ 'hexdump', ] + inputs
subprocess.Popen( args, cwd='/foo/bar' )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.