日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

os.system() 和 os.popen()

發布時間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 os.system() 和 os.popen() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.os.popen(command[,?mode[,?bufsize]])
?
?os.system(command)

2.os.popen() 功能強于os.system() , os.popen() 可以返回回顯的內容,以文件描述符返回。
eg:
t_f = os.popen ("ping 192.168.1.1")
print t_f.read()

或者:
for line in os.popen("dir"):
????print line

?

最近在做那個測試框架的時候發現 Python 的另一個獲得系統執行命令的返回值和輸出的類。

最開始的時候用 Python 學會了 os.system() 這個方法是很多比如 C,Perl 相似的。

os.system('cat /proc/cpuinfo')


但是這樣是無法獲得到輸出和返回值的,繼續 Google,之后學會了 os.popen()。

output?=?os.popen('cat /proc/cpuinfo')
print?output.read()


通過 os.popen() 返回的是 file read 的對象,對其進行讀取 read() 的操作可以看到執行的輸出。但是怎么讀取程序執行的返回值呢,當然咯繼續請教偉大的 Google。Google 給我指向了?commands — Utilities for running commands。
這樣通過 commands.getstatusoutput() 一個方法就可以獲得到返回值和輸出,非常好用。

(status,?output)?=?commands.getstatusoutput('cat /proc/cpuinfo')
print?status,?output

Python Document 中給的一個例子,很清楚的給出了各方法的返回。

?

>>>?import?commands
>>>?commands.getstatusoutput('ls /bin/ls')
(0,?'/bin/ls')
>>>?commands.getstatusoutput('cat /bin/junk')
(256,?'cat: /bin/junk: No such file or directory')
>>>?commands.getstatusoutput('/bin/junk')
(256,?'sh: /bin/junk: not found')
>>>?commands.getoutput('ls /bin/ls')
'/bin/ls'
>>>?commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'

?

os.system 調用系統命令,完成后退出,返回結果是命令執行狀態,一般是0

os.popen?可以實現一個“管道”,從這個命令獲取的值可以在python 中繼續被使用

os.popen使用語法如下:

os.popen('CMD').readlines()[0]

?

?

最近有個需求就是頁面上執行shell命令,第一想到的就是os.system,

復制代碼代碼如下:
os.system('cat /proc/cpuinfo')


但是發現頁面上打印的命令執行結果 0或者1,當然不滿足需求了。

?

嘗試第二種方案 os.popen()

復制代碼代碼如下:
output = os.popen('cat /proc/cpuinfo')
print output.read()


通過 os.popen() 返回的是 file read 的對象,對其進行讀取 read() 的操作可以看到執行的輸出。但是無法讀取程序執行的返回值)

?

嘗試第三種方案 commands.getstatusoutput() 一個方法就可以獲得到返回值和輸出,非常好用。

復制代碼代碼如下:
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output


Python Document 中給的一個例子,

復制代碼代碼如下:
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'

?

Python os.popen() Method

Description

The method?popen()?opens a pipe to or from command.The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.The bufsize argument has the same meaning as in?open()?function.

Syntax

Following is the syntax for?popen()?method:

os.popen(command[, mode[, bufsize]])

Parameters

  • command?-- This is command used.

  • mode?-- This is the Mode can be 'r'(default) or 'w'.

  • bufsize?-- If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).?

Return Value

This method returns an open file object connected to the pipe.

Example

The following example shows the usage of popen() method.

# !/usr/bin/pythonimport os, sys# using command mkdir
a = 'mkdir nwdir'b = os.popen(a,'r',1)print b

When we run above program, it produces following result:

open file 'mkdir nwdir', mode 'r' at 0x81614d0


調用c程序:
fd=os.popen('/path/to/alignment arg1 arg2 arg3')
output=fd.read()
fd.close()

轉載于:https://www.cnblogs.com/jefree/p/4461979.html

總結

以上是生活随笔為你收集整理的os.system() 和 os.popen()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产调教一区 | 99re这里只有精品在线观看 | 久久亚洲AV无码专区成人国产 | 成人av高清在线观看 | 永久免费无码av网站在线观看 | 乳揉みま痴汉4在线播放 | 久久久高清免费视频 | 亚洲天堂欧美在线 | 欧美一区二区三区视频 | 国产尤物av | 国产不卡a | 亚洲欧美经典 | 亚洲国产精品第一页 | 精品国产欧美一区二区三区成人 | 国产一区亚洲二区 | 亚洲免费三区 | 欧美精品入口蜜桃 | 快色视频在线观看 | 裸体美女免费视频网站 | 色超碰 | 麻豆精品在线视频 | 亚洲欧美激情图片 | 嫩草影院懂你的影院 | 欧美啊v| 成人午夜性视频 | 俄罗斯精品一区二区三区 | 综合色伊人| 在线看日本 | 美女国产精品 | 毛片高清免费 | 久久久久久久久久久97 | 黄色一级片免费 | 日本黄色免费观看 | xx色综合 | 性xxxxbbbb | 国产精品免费一区二区三区在线观看 | av黄色在线免费观看 | 一卡二卡三卡在线观看 | 日本a级片网站 | 日本成人在线免费观看 | 日本黄色免费网址 | 奇米影视亚洲春色 | 91丝袜在线 | 亚洲操一操 | 婷婷在线网 | 日日夜夜噜 | 在线观看v片 | 好男人资源 | 97超碰中文字幕 | 99成人精品 | 国产原创麻豆 | 性喷潮久久久久久久久 | 污片在线看 | av的天堂 | 久久精品国产大片免费观看 | 久久久无码精品亚洲无少妇 | 欧美亚洲国产视频 | 亚洲国产果冻传媒av在线观看 | 可以在线观看av的网站 | 天天做天天爽 | xxxx日本免费 | 日本中文字幕在线看 | 亚洲精品国产精 | 北条麻妃一区二区三区四区五区 | 五月天丁香社区 | 免费一级片在线观看 | 国产午夜精品一区 | 亚洲av日韩av在线观看 | 一本大道久久a久久综合婷婷 | 欧美成人精品一区二区男人看 | 国产综合欧美 | 久久久久久久久久久国产精品 | 日韩在线| 久久六六 | 国产三级麻豆 | 在线观看欧美 | 激情五月俺也去 | 国产精品1234 | av小片| 五月天丁香网 | 免费黄色成人 | 悟空影视大全免费高清观看在线 | 五月天开心网 | av福利院| 日本免费网站在线观看 | 天天操人人干 | 爱插视频 | 欧洲av一区二区 | 欧美区二区三区 | 熟睡人妻被讨厌的公侵犯 | 特种兵之深入敌后高清全集免费观看 | 国产妇女馒头高清泬20p多 | 日韩在线你懂的 | 青青操视频在线观看 | 日日夜夜精 | 久久久久久久人妻无码中文字幕爆 | 五月婷影院 | av一区二区三区在线 | 中文字幕无产乱码 |