Re: [問題] 關於使用者輸入資料

看板Python作者 (大頭熊)時間17年前 (2008/06/30 20:45), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/4 (看更多)
#!/usr/bin/env python ###test.py import sys inpFile=open(sys.argv[1],'r') line=inpFile.readline() while line!="": print line line=inpFile.readline() inpFile.close() 使用方法: python test.py num.txt ※ 引述《Xphenomenon (啦 )》之銘言: : 在 c 裡面可以這樣寫 : test.c : #define : int main(void) { : int num; : while(scanf("%d", &num) == 1) { : printf("%d\n", num); : } : return 0; : } : num.txt 內容: : 3 : 3 : 5 : 6 : 7 : 8 : gcc -o test test.c : ./test < num.txt : 請問如果在 Python 裡面我要怎麼寫才能達到如 C 的要求呢? : 我試過如: : test.py : while True: : a = raw_input() : if(len(a) : print a : python test.py < num.txt : 但會有以下錯誤: : Traceback (most recent call last): : File "test.py", line 2, in <module> : a = raw_input() : EOFError: EOF when reading a line : 麻煩大大解答一下,感謝 :> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.109.228.172

07/01 10:42, , 1F
感謝回答 :>
07/01 10:42, 1F
文章代碼(AID): #18QDLWKf (Python)
討論串 (同標題文章)
文章代碼(AID): #18QDLWKf (Python)