我正在使用 subprocess 模块在 python 中运行命令。但问题是我还想在命令中包含一个字符串(用于文件名)。
我想做的一个例子:
from subprocess import call
command = "cd/DirectoryName"
call = [(command)]
在这个特定示例中,我希望 DirectoryName 是由用户确定的变量。
我尝试过的无济于事:
Desktop=raw_input()
cmd="'cd %s'(Desktop/)"
call([cmd])
这是我尝试在 python shell 中运行这些命令时遇到的错误。
Chicken='Chicken'
command = 'say %s' % (Chicken)
print command
say Chicken
call([command])
Traceback (most recent call last):
File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
刚试过这个,它使shell崩溃。
Chicken="Chicken"
print Chicken
Chicken
call[("say %s" % (Chicken)]