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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM