zl程序教程

您现在的位置是:首页 >  系统

当前栏目

os.system获取返回值 python3 cmd 获取返回值

OSPython3 获取 cmd system 返回值
2023-09-27 14:27:48 时间

os.system是无法获取返回值的 只返回一个cmd

可以使用

   (status, uploadRes) = subprocess.getstatusoutput(cmd)
import subprocess

不是os.subprocess 要记得导入上面的包

返回内容上面是这些下面是我们想要的

 88 32.8M    0     0   88 29.1M      0  1495k  0:00:22  0:00:19  0:00:03 1510k
 93 32.8M    0     0   93 30.5M      0  1491k  0:00:22  0:00:20  0:00:02 1485k
 97 32.8M    0     0   97 32.0M      0  1495k  0:00:22  0:00:21  0:00:01 1485k
100 32.8M    0     0  100 32.8M      0  1431k  0:00:23  0:00:23 --:--:-- 1223k
100 32.8M    0     0  100 32.8M      0  1372k  0:00:24  0:00:24 --:--:--  946k
100 32.8M  100   644  100 32.8M     26  1358k  0:00:24  0:00:24 --:--:--  781k{"-----------------

"}}

返回的内容可能包含不是你想要的 记得截取

    print("==============\n")
    print(str(uploadRes)+"\n")
    uploadRes="{"+re.findall("k{(.*?)\"}}",uploadRes)[0]+"\"}}"
    print("==============\n")
    print(str(uploadRes)+"\n")
    print("==============\n")