python外部程序调用(命令行窗口)

1.阻塞式(阻塞主线程)

os.system

import os
res = os.system('mspaint')
#ret是返回值,0表示执行成功,2表示执行失败
if ret==0:
  print('file copied')
else:
  print('copy file failed!')
print(
'after call')

2.非阻塞式(不阻塞主线程)

subprocess

from subprocess import PIPE,Popen
process = Popen(
  'dir c:',
  stdin=None,
  stdout='PIPE', #输出管道重定向
  stderr=None,
  shell=True
)
#取出运行输出结果 并打印
output,err = process.communicate()
print(output)

 

posted @ 2019-08-28 21:40  等一念  阅读(2512)  评论(0编辑  收藏  举报