python os system_python中os. popen system的区别
python調(diào)用Shell腳本或者是調(diào)用系統(tǒng)命令,有兩種方法:
os.system(cmd)或os.popen(cmd),前者返回值是腳本的退出狀態(tài)碼,正確會(huì)返回0,錯(cuò)誤會(huì)返回其他數(shù)字。
后者的返回值是腳本執(zhí)行過程中的輸出內(nèi)容。實(shí)際使用時(shí)視需求情況而選擇。
popen的返回時(shí)一個(gè)對(duì)象,直接查看,是這樣的結(jié)果:
popen要想查看結(jié)果需要這樣查看:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
f = os.popen("ls -l /root", "r")
print f
for line in f:
print line.strip()
這個(gè)返回的結(jié)果是:
[root@abc301 tmp]# python a.py
total 432
-rwxr-xr-x 1 root root 10773 Oct 23 2017 623get.sh
-rwxr-xr-x 1 root root 11 Mar 12 2018 aa.sh
-rw------- 1 root root 1960 Nov 2 2016 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Oct 24 2017 a.out
drwxr-xr-x 2 root root 4096 Nov 3 2017 awr
drwxr-xr-x 14 root root 4096 Oct 23 2017 back
下面是使用system的案例:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
aaa = os.system("date")
print aaa
運(yùn)行結(jié)果:
[root@tcas301 tmp]# python a.py
Tue Mar 19 13:27:41 CST 2019
0
總結(jié)
以上是生活随笔為你收集整理的python os system_python中os. popen system的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从写实数字人切入超写实数字人
- 下一篇: 虽然屏保不再保屏屏保没有了