python执行shell命令
2012-08-17 21:55
302 查看
Here's a summary of the ways to call external programs and the advantages and disadvantages of each:
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.html
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.html
The
instead of
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.html
The
http://docs.python.org/lib/node529.html
The 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.
我还用过 commands.getoutput("ls")命令执行完之后的输出
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.html
stream = os.popen("some_command with args")will do the same thing as
os.systemexcept 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.html
The
Popenclass of the
subprocessmodule. This is intended as a replacement for
os.popenbut has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you'd say
print Popen("echo Hello World", stdout=PIPE, shell=True).stdout.read()
instead of
print 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.html
The
callfunction from the
subprocessmodule. This is basically just like the
Popenclass 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.html
The 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.
我还用过 commands.getoutput("ls")命令执行完之后的输出
相关文章推荐
- python 执行shell命令
- python-Django中设置操作shell时执行的数据库命令在控制台显示
- python执行使用shell命令方法分享
- python执行shell命令的几种方法
- python执行shell命令
- Python 执行Shell 外部命令
- python中执行shell命令的几个方法小结
- python执行shell命令
- python学习——python中执行shell命令
- python中执行shell命令的几个方法小结
- python中执行shell命令
- 【python运维】python执行shell命令的4种方法
- python中执行shell命令的几种方式
- python(6)-执行shell命令
- python执行shell命令四法
- 用python SSH模块登录,并在远程机执行shell命令
- 获取python执行shell命令的结果
- python之执行shell命令
- 转变思维--使用Python生成Shell命令,批量执行程序
- python-执行shell命令的模块使用