作者Morneau (累了)
看板Python
标题Re: [问题] 关於使用者输入资料
时间Mon Jun 30 21:25:15 2008
sys.stdin 是一个档案物件,等同於 C 的 stdio.h 里的 stdin
所以直接读取 sys.stdin 就好罗
#!/usr/bin/python -t
import sys
while True:
str = sys.stdin.readline()
str = str[:-1]
if len(str) > 0:
print str
else:
sys.exit(0)
另外根据官方手册, raw_input() 读到EOF就会产生 EOFError 例外
所以会产生错误是正常的
※ 引述《jameschen113 (大头熊)》之铭言:
: #!/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: 61.230.206.149
1F:推 Xphenomenon:感谢回答 :> 07/01 10:44