2012-04-21 109 views
1

嘿傢伙我試圖在Python中使用參數變量,但無法在終端執行程序。TypeError:必須是字典,而不是str

計劃:

from sys import argv 

script,first,second,third = argv 

print "The script is called:", script 
print "Your first variable is:", first 
print "Your second variable is:", second 
print "Your third variable is:", third 

輸出:

>>> execfile("lesson13.py","dog","cat") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: must be dict, not str 
>>> 
+0

該錯誤與程序無關,您只是誤用了'execfile'。 – delnan 2012-04-21 18:04:05

回答

4

execfile()不帶命令的參數。請嘗試使用subprocess

+0

下面是同一個問題的SE問題:http://stackoverflow.com/questions/5788891/execfile-with-argument-in-python-shell – CppLearner 2012-04-21 18:13:08

+0

>>> subprocess.call('lesson13.py','kill 」,‘比爾’,‘金’) 回溯(最近通話最後一個): 文件‘’,1號線,在 NameError:名字‘子’沒有定義 – user1345589 2012-04-22 11:41:58

+0

我仍然無法得到它的工作:( – user1345589 2012-04-22 11:42:17

相關問題