【问题标题】:TypeError: must be dict, not strTypeError:必须是 dict,而不是 str
【发布时间】:2012-04-21 17:59:56
【问题描述】:

大家好,我尝试在 Python 中使用参数变量,但无法在终端执行程序。

计划:

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
>>> 

【问题讨论】:

  • 错误与程序无关,你只是误用了execfile

标签: python


【解决方案1】:

execfile() 不接受命令参数。尝试改用subprocess

【讨论】:

  • 这是针对同一问题的 SE 问题:stackoverflow.com/questions/5788891/…
  • >>> subprocess.call('lesson13.py','kill','bill','kim') Traceback(最近一次调用最后):文件“”,第 1 行, 在 NameError: name 'subprocess' is not defined
  • subprocess is not defined 你导入了吗?如果您仍然无法解决,请更新您的帖子!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
相关资源
最近更新 更多