Re: [問題] os.system輸入輸出

看板Python作者 (Ar藤)時間17年前 (2008/08/13 16:13), 編輯推噓3(303)
留言6則, 3人參與, 最新討論串3/4 (看更多)
※ 引述《liangjr (aaa)》之銘言: : ※ 引述《Arton0306 (Ar藤)》之銘言: : : 如果我有一程式gcd.exe : : 執行後使用者輸入兩數字enter後輸出其gcd 接著關閉 : : 這樣是否有辦法利用os.system或其它函式 : : 讓python中 兩變數x,y當輸入 : : 而z儲存輸出 : : (用檔案當中介 利用管線來執行 "之外的其它方法") : 類似這樣? : def gcd(x, y): : gcd = subprocess.Popen("gcd.exe", : stdin=subprocess.PIPE, : stdout.subprocess.PIPE) : gcd.stdin.write(x) : gcd.stdin.write(y) : return gcd.stdout.read() 對對 就是這個 感謝! 不過stdin.write不知怎麼用 我查文件http://docs.python.org/lib/node532.html 改成用communicate可以跑 程式碼如下 import subprocess def twosum(x, y): twosum = subprocess.Popen("twosum.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE) return twosum.communicate(str(x)+' '+str(y)) a=10 b=23 k=twosum(a,b) print 'k='+k[0] -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.138.142 ※ 編輯: Arton0306 來自: 220.135.138.142 (08/13 16:13)

08/13 18:35, , 1F
這還是用到了管線
08/13 18:35, 1F

08/13 18:40, , 2F
主要是不希望有檔案當中介 所以ok
08/13 18:40, 2F

08/13 22:31, , 3F
pipe和檔案是兩回事吧
08/13 22:31, 3F

08/14 06:23, , 4F
我是指不知l大的方法時 我可以用檔案和pipe做出類似效果
08/14 06:23, 4F

08/14 06:25, , 5F
如twosum < input > output input存a,b ouput存k
08/14 06:25, 5F

08/14 19:28, , 6F
通常我會叫這個是 IO redirection ...
08/14 19:28, 6F
文章代碼(AID): #18efULHq (Python)
討論串 (同標題文章)
文章代碼(AID): #18efULHq (Python)