#Python#Python调用系统命令
来源:互联网 发布:九云图软件下载 编辑:程序博客网 时间:2024/05/21 04:26
转帐自:http://stackoverflow.com/questions/89228/calling-an-external-command-in-python
Here's a summary of the ways to call external programs and the advantages and disadvantages of each:
os.system("some_command with args")
passes the command and arguments to your system's shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example,os.system("some_command < input_file | another_command > output_file")
However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs.
http://docs.python.org/lib/os-process.htmlstream = os.popen("some_command with args")
will do the same thing asos.system
except that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don't need to worry about escaping anything.
http://docs.python.org/lib/os-newstreams.htmlThe
Popen
class of thesubprocess
module. This is intended as a replacement foros.popen
but has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you'd sayprint Popen("echo Hello World", stdout=PIPE, shell=True).stdout.read()
instead ofprint os.popen("echo Hello World").read()
but it is nice to have all of the options there in one unified class instead of 4 different popen functions.
http://docs.python.org/lib/node528.htmlThe
call
function from thesubprocess
module. This is basically just like thePopen
class and takes all of the same arguments, but it simply wait until the command completes and gives you the return code. For example:return_code = call("echo Hello World", shell=True)
http://docs.python.org/lib/node529.htmlThe os module also has all of the fork/exec/spawn functions that you'd have in a C program, but I don't recommend using them directly.
- #Python#Python调用系统命令
- python调用系统命令
- python--调用系统命令
- python调用系统命令
- Python 调用系统命令
- python调用系统命令
- python 调用linux系统命令
- python调用linux系统命令
- python调用系统命令的方法
- python 调用Linux系统命令笔记
- python 调用系统命令,执行命令行
- python可以调用外部系统命令
- python调用shell命令
- Python调用shell命令
- Python调用Dos命令
- python调用shell命令
- python调用shell命令
- python调用shell命令
- Mongodb CSharp Driver Tutorial
- JS函数表达式 只运行一次
- Windows消息响应机制之四:PostQuitMessage和GetMessage函数
- 大话西游·优秀的团队
- 前台框架之-ext
- #Python#Python调用系统命令
- python 学习列表
- VMware命令行介绍
- js 页面跳转等等,其他功能
- 汇编语言笔记
- 数学问题(六)之 求100以内最大的素数
- Shell(希尔)排序的分析和比较
- 使用xlib编写简单的程序
- Memory marshalling in Windows CE