# -*- coding:utf-8 -*- # -Author: Paulliam - import os cmd_result = os.system("dir") #执行命令后,并不能保存结果 print("the value is:",cmd_result) #打印命令执行后的返回结果 print("-----------------------------------------") cmd_result = os.popen("dir") #执行命令后,cmd_result的值只是一个对象的地址,这个对象正是一个命令执行的结果 print("the value is:",cmd_result) print("-----------------------------------------") cmd_result = os.popen("dir").read() #执行命令后,能够保存结果,并将结果赋值给cmd_result print("the value is:",cmd_result) #打印命令执行后保存的结果
######运行结果:
"D:\Program Files\python-3.6.4\python.exe" C:/Users/Administrator/Desktop/python-prj/sys.py
������ C �еľ�û�б�ǩ��
�������� 3C69-84C5
C:\Users\Administrator\Desktop\python-prj ��Ŀ¼
2018/02/26 21:55 <DIR> .
2018/02/26 21:55 <DIR> ..
2018/02/26 21:55 <DIR> .idea
2018/02/11 18:59 423 coding.py
2018/02/25 14:26 205 continue.py
2018/02/25 13:30 196 for double.py
2018/02/25 14:43 971 guess for.py
2018/02/23 22:05 553 guess.py
2018/02/25 14:36 1,002 guess1.py
2018/02/11 20:04 520 interaction.py
2018/02/22 11:36 839 interaction1.py
2018/02/23 22:24 634 passwd.py
2018/02/26 21:55 888 sys.py
2018/02/23 21:37 231 while.py
2018/02/25 12:43 228 while1-for.py
12 ���ļ� 6,690 �ֽ�
3 ��Ŀ¼ 100,031,672,320 �����ֽ�
the value is: 0
-----------------------------------------
the value is: <os._wrap_close object at 0x014024D0>
-----------------------------------------
the value is: 驱动器 C 中的卷没有标签。
卷的序列号是 3C69-84C5
C:\Users\Administrator\Desktop\python-prj 的目录
2018/02/26 21:55 <DIR> .
2018/02/26 21:55 <DIR> ..
2018/02/26 21:55 <DIR> .idea
2018/02/11 18:59 423 coding.py
2018/02/25 14:26 205 continue.py
2018/02/25 13:30 196 for double.py
2018/02/25 14:43 971 guess for.py
2018/02/23 22:05 553 guess.py
2018/02/25 14:36 1,002 guess1.py
2018/02/11 20:04 520 interaction.py
2018/02/22 11:36 839 interaction1.py
2018/02/23 22:24 634 passwd.py
2018/02/26 21:55 888 sys.py
2018/02/23 21:37 231 while.py
2018/02/25 12:43 228 while1-for.py
12 个文件 6,690 字节
3 个目录 100,031,672,320 可用字节
Process finished with exit code 0