可能的重复:
在Python中调用外部命令 https://stackoverflow.com/questions/89228/calling-an-external-command-in-python
我想使用 python 在另一个目录中运行命令。
为此有哪些不同的方法,哪一种是最有效的?
我想做的是如下,
cd dir1
execute some commands
return
cd dir2
execute some commands
当然,如果您只想通过 python 在 shell 上运行一个(简单的)命令,您可以通过system
的功能os
模块。例如:
import os
os.system('touch myfile')
如果您想要更复杂的东西来更好地控制命令的执行,请继续并使用subprocess
其他人在这里建议的模块。
欲了解更多信息,请点击以下链接:
- Python 官方文档os.system() http://docs.python.org/2/library/os.html#os.system
- Python 官方文档subprocess module http://docs.python.org/2/library/subprocess.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)