我将尝试读取通过命令行重定向stdin的文本文件的内容,并在接收方需要将其重新组装为原始形式时通过互联网发送它。
例如:
例如:
$ python test.py < file.txt
我尝试使用以下受链接启发的代码读取文件并将其重新组装:
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!"
但是这个解决方案只有在我没有空行(两个'\n'相连)的情况下才有效,如果我有空行,我的循环就会中断,文件读取也就结束了。我该怎么读取标准输入直到达到被重定向的文件的结尾符号<>?
file.write(sys.stdin.read())
- William Pursell