2013-06-19 32 views
0

以下命令行工作,並給出正確的結果python腳本的命令行運行,但使用子給出了錯誤

$ python maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001 

但是當確切的命令被調用時,使用下面的子模塊:

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' ]) 

它給出了以下錯誤:

/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 

是什麼原因?命令完全相同。 謝謝你的幫助。

回答

2

當使用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' ]) 

什麼,你現在有將是等同於運行在以下命令行:

python 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001' 
+0

非常感謝。 – BobbyF

1

除了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) 
+0

感謝您的幫助。 – BobbyF

相關問題