python执行第三方命令

主要有os.system(),os.popen()和commands.getstatusoutput()三个命令。

python文档的解释如下:

os.system()

Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system() , and has the same limitations. Changes to sys.stdin , etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait() . Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command , given by the Windows environment variable COMSPEC : on command.com systems (Windows 95, 98 and ME) this is always 0 ; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.

Availability: Unix, Windows.

通过创建一个子进程来执行命令。调用c的system函数。

在unix机器上,在POSIX标准中,返回值类型由系统决定。

 

commands.getstatusoutput()

    Execute the string cmd in a shell with os.popen() and return a 2-tuple (status, output) . cmd is actually run as { cmd ; }   2>&1 , so that the returned output will contain output or error messages. A trailing newline is stripped from the output. The exit status for the command can be interpreted according to the rules for the C function wait() .

 

返回值为返回状态和命令的输出结果。保存在一维数组中。其中输出结果包含错误信息输出。

 

 

 

 

 

 

 

 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值