看板 Python 關於我們 聯絡資訊
※ 引述《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)
yungyuc:這還是用到了管線 08/13 18:35
Arton0306:主要是不希望有檔案當中介 所以ok 08/13 18:40
legnaleurc:pipe和檔案是兩回事吧 08/13 22:31
Arton0306:我是指不知l大的方法時 我可以用檔案和pipe做出類似效果 08/14 06:23
Arton0306:如twosum < input > output input存a,b ouput存k 08/14 06:25
legnaleurc:通常我會叫這個是 IO redirection ... 08/14 19:28