2019-09-09 python调用外部程序

1.wget--用于下载;ffmpeg--多媒体处理(音频,视频);free

2.调用外部程序两种方法:

os.system:(标准库中的非内置库)=Windows下的命令行

例如:

import os

os.system("mspaint")

print('after call')

subprocess:

import subprocess
res = subprocess.check_output('dir',shell=True)
print(res.decode('gbk'))

from subprocess import Popen
popen = Popen(
args = 'mspaint e:\33.jpg',
shell= True
)
print("done")

from subprocess import Popen,PIPE
popen = Popen(
args = 'mspaint e:\33.jpg',
stdout= PIPE,
stderr= PIPE,
shell= True,
encoding= 'gbk'
)
output,err = popen.communicate()
print(output)
print('------------')
print(err)

3.返回值

Windows:

.exe程序的退出码是程序的退出

Linux:

判断返回码是不是0;如果是1则失败;

例如:

import os

res = os.system('cp /opt/file1 /home/jcy/file1')

if res ==0:

  print('file copied.')

else:

  print('copy file failed!!')

4.

原文地址:https://www.cnblogs.com/baiyy/p/11490449.html