subprocess 模块 - 图1


    1. import subprocess # 子进程的意思
    2. obj = subprocess.Popen(r'dir F:\学习课件\python\项目', shell=True, # 如果是mac,'ls /'
    3. stdout=subprocess.PIPE, # 把正确结果放到管道里
    4. stderr=subprocess.PIPE, # 把错误结果放到管道里
    5. )
    6. # print(obj)
    7. res = obj.stdout.read()
    8. print(res.decode('GBK')) # 使用计算机系统的解码格式
    9. '''
    10. D:\python3.8.5\python.exe F:/学习课件/python/练习/run.py
    11. 驱动器 F 中的卷是 工作
    12. 卷的序列号是 16DA-0A10
    13. F:\学习课件\python\项目 的目录
    14. 2020/12/17 14:47 <DIR> .
    15. 2020/12/17 14:47 <DIR> ..
    16. 2020/12/16 21:11 <DIR> ATM
    17. 2020/12/17 10:48 29,755 ATM.zip
    18. 1 个文件 29,755 字节
    19. 3 个目录 177,912,426,496 可用字节
    20. Process finished with exit code 0
    21. '''
    22. # res1 = obj.stderr.read()
    23. # print(res1.decode('GBK')) # 如果有文件为空,没有文件就显示,找不到文件