[問題] 有關subprocess的問題

看板Python作者 (阿鈣鈣~)時間10年前 (2014/02/23 15:38), 編輯推噓0(0010)
留言10則, 2人參與, 最新討論串1/1
小弟我最近再trace一份code 在很多個函式內看到 file=subprocess.Popen(instruction, shell=True,stdout=subprocess.PIPE).stdout 有查了subprocess內很多函式的用法 但組起來不管怎麼看都感覺不通順 不是很懂到底是甚麼結果 我的instruction是一行unix shell script 不過在file那行之後做迴圈 for line in file 依照小弟的理解 那是一行行讀取剛剛file內的值吧? 不過我input的是一行的unix shell script 為什麼要一行行讀取? file那行到底做了甚麼事@@? 還有可以在問一下PIPE到底是甚麼嗎? 看了也不懂在做甚麼... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.25.118.134

02/23 22:42, , 1F
這行的意思是:起一個shell subprocess,執行instruction
02/23 22:42, 1F

02/23 22:44, , 2F
的內容,然後把結果的stdout assign給file.
02/23 22:44, 2F

02/23 22:46, , 3F
給它.PIPE的話,可以分別在Popen的stdin, stdout, stderr
02/23 22:46, 3F

02/23 22:47, , 4F
這三個attributes拿到它的input, output, error.
02/23 22:47, 4F

02/23 22:49, , 5F
後面假設file多行可能是該instruction的output不止一行.
02/23 22:49, 5F

02/24 00:30, , 6F
所以他會輸出一個output file然後裡面是我的instruction囉
02/24 00:30, 6F

02/24 00:34, , 7F
? 所以Popen內只是設定attribute然後.PIPE拿到的output應
02/24 00:34, 7F

02/24 00:35, , 8F
該會是怎樣還是不太清楚欸
02/24 00:35, 8F

02/24 09:25, , 9F
你誤會這裡的file了。光看這行code,file只是變數名稱。
02/24 09:25, 9F

02/24 09:27, , 10F
裡面裝instruction執行完的stdout,應該是一個多行的str。
02/24 09:27, 10F
文章代碼(AID): #1J2QMA0x (Python)