哪有什么岁月静好,不过是有人替你负重前行!

subprocess实例

import subprocess
a = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)  #stdout输出的意思,subprocess管道的意思,PIPE子进程。
print(a)  #结果为:<Popen: returncode: None args: ['d', 'i', 'r']>
#print(a.stdout.read())  #结果为:b' \xc7\xfd\xb6\xaf\xc6\xf7 E \xd6\xd0\xb5\xc4\xbe\xed\xca\xc7 \xc8\xed\xbc\xfe\r\n \xbe\xed\xb5\xc4\xd0\xf2\xc1\xd0\xba\xc5\xca\xc7 D4FA-828F\r\n\r\n E:\\Python project\\pythonProject\\week7\\day26 \xb5\xc4\xc4\xbf\xc2\xbc\r\n\r\n2021/10/12  15:45    <DIR>          .\r\n2021/10/12  15:45    <DIR>          ..\r\n2021/10/11  17:44               173 client.py\r\n2021/10/11  17:38               245 serve.py\r\n2021/10/12  15:45               442 test.py\r\n               3 \xb8\xf6\xce\xc4\xbc\xfe            860 \xd7\xd6\xbd\xda\r\n               2 \xb8\xf6\xc4\xbf\xc2\xbc 388,668,350,464 \xbf\xc9\xd3\xc3\xd7\xd6\xbd\xda\r\n'
print(str(a.stdout.read(),'gbk'))

上述代码执行结果为:

<Popen: returncode: None args: ['d', 'i', 'r']>
 驱动器 E 中的卷是 软件
 卷的序列号是 D4FA-828F

 E:\Python project\pythonProject\week7\day26 的目录

2021/10/12  15:46    <DIR>          .
2021/10/12  15:46    <DIR>          ..
2021/10/11  17:44               173 client.py
2021/10/11  17:38               245 serve.py
2021/10/12  15:46             1,150 test.py
               3 个文件          1,568 字节
               2 个目录 388,668,346,368 可用字节

 

posted @ 2021-10-12 15:49  longfei825  阅读(161)  评论(0编辑  收藏  举报