我想使用os.system运行命令,但出现错误
c:/fe ' is not recognized as an internal or external command, operable program or batch file
我使用的代码是
import os
os.system('"C:\\fe re\\python.exe" program "c:\\test now\\test.txt" http://site.to.explore')
如果我只运行,它将起作用:
import os
os.system('"C:\\fe re\\python.exe" program -h')
或者如果我在python路径中没有这样的空间
import os
os.system('C:\\fere\\python.exe program "c:\\test now\\test.txt" http://site.to.explore')
但是,如果我在python路径和txt路径中的命令中都有两双双引号,则会出现错误...
os.system
有一些严重的缺点,尤其是文件名中的空格和wrt安全性。我建议您研究一下subprocess
模块,尤其是subprocess.check_call,它的功能要强大得多。然后你可以做例如
import subprocess
subprocess.check_call(["c:\\fe re\\python.exe", "program", etcetc...])
当然,除非用户已经从命令行使用相同的特权自己运行了脚本,否则请务必小心不要在这些调用中包含用户定义的变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句