在Python中调用外部命令 [英] Calling an external command in Python
本文介绍了在Python中调用外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从Python脚本中调用外部命令(就像我在Unix shell或Windows命令提示符下键入它)?
How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
推荐答案
查看stdlib中的子流程模块 :
Look at the subprocess module in the stdlib:
from subprocess import call
call(["ls", "-l"])
子进程与系统的优点是,灵活(你可以得到stdout,stderr,真正的状态代码,更好的错误处理等)。我认为 os.system
已被弃用,或将被弃用。请参阅使用子过程模块。
The advantage of subprocess vs system is that it is more flexible (you can get the stdout, stderr, the "real" status code, better error handling, etc...). I think os.system
is deprecated, too, or will be. See Replacing Older Functions with the subprocess Module.
这篇关于在Python中调用外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文