[英]python script runs from command line but using subprocess gives error
the following command line works and gives correct results 以下命令行有效并给出正确的结果
$ python maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001
but when the exact command is called, using the following subprocess module: 但是在调用确切命令时,请使用以下子流程模块:
run=subprocess.Popen([sys.executable, 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001' ])
it gives the following error: 它给出以下错误:
/usr/bin/python: can't open file 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001': [Errno 2] No such file or directory
what's the reason? 什么原因? the commands are exactly the same.
命令完全相同。 Thank you for your help.
谢谢您的帮助。
When using subprocess.Popen()
the first argument should be a list with a separate entry for each argument to the process you want to run: 使用
subprocess.Popen()
,第一个参数应该是一个列表,其中包含要运行的进程的每个参数的单独条目:
run=subprocess.Popen([sys.executable, 'maps2.py', '-i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0', '-o=temp/CMIP3', '-p=temp_001' ])
What you currently have would be the equivalent to running the following on the command line: 您当前拥有的内容等同于在命令行上运行以下内容:
python 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001'
In addition to FJ's answer, you can easily split the name of the executable from the arguments with shlex.split
除了FJ的答案,您还可以使用
shlex.split
从参数中轻松拆分可执行文件的名称。
mapsCommand = 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001'
fullCommand = [sys.executable]
fullCommand.extend(shlex.split(mapsCommand))
run=subprocess.Popen(fullCommand)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.