使用python从重定向的stdin读取文件

use*_*551 5 python shell stdin

我试图通过命令行读取重定向stdin的文本文件的内容,并在接收器必须将其组装回原始形式时通过Internet发送它.

例如:

$ python test.py < file.txt
Run Code Online (Sandbox Code Playgroud)

我试图读取该文件,并把它与灵感来自于下面的代码汇编回链接:

for line in sys.stdin:
  stripped = line.strip()
  if not stripped: break
  result = result + stripped

print "File is beeing copied"
file = open("testResult.txt", "w")
file.write(result)
file.close()
print "File copying is complete!"
Run Code Online (Sandbox Code Playgroud)

但是这个解决方案可以工作,只要我没有空行(两个'\n'一个接一个)在我的文件中,如果我有,我的循环中断和文件读取结束.我怎样才能从stdin读取我到达重定向文件的<>?

Wil*_*ell 5

为什么你甚至看数据:

result = sys.stdin.read()
Run Code Online (Sandbox Code Playgroud)