Python os.system 和 os.popen的區別

來源:互聯網
上載者:User

標籤:

(1) os.system

# 僅僅在一個子終端運行系統命令,而不能擷取命令執行後的返回資訊

system(command) -> exit_status
Execute the command (a string) in a subshell.

# 如果再命令列下執行,結果直接列印出來

1 >>> os.system(‘ls‘)2 3 04101419778.CHM   bash      document    media      py-django   video4 5 11.wmv            books     downloads   Pictures  python6 7 all-20061022      Desktop   Examples    project    tools

(2) os.popen

# 該方法不但執行命令還返回執行後的資訊對象

popen(command [, mode=‘r‘ [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.

例如:

 1 >>>tmp = os.popen(‘ls *.py‘).readlines() 2  3 >>>tmp 4  5 Out[]: 6  7 [‘dump_db_pickle.py ‘, 8  9 ‘dump_db_pickle_recs.py ‘,10 11 ‘dump_db_shelve.py ‘,12 13 ‘initdata.py ‘,14 15 ‘__init__.py ‘,16 17 ‘make_db_pickle.py ‘,18 19 ‘make_db_pickle_recs.py ‘,20 21 ‘make_db_shelve.py ‘,22 23 ‘peopleinteract_query.py ‘,24 25 ‘reader.py ‘,26 27 ‘testargv.py ‘,28 29 ‘teststreams.py ‘,30 31 ‘update_db_pickle.py ‘,32 33 ‘writer.py ‘]

好處在於:將返回的結果賦於一變數,便於程式的處理。

Python os.system 和 os.popen的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.