我正嘗試讀取通過命令行重定向stdin的文本文件的內容,並在接收方將其組裝回原始格式時通過Internet發送。從python重定向stdin讀取文件
例如:
$ python test.py < file.txt
我試圖讀取該文件,並用下面的代碼通過link啓發組裝回去:
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!"
但只要使用這種解決方案,因爲我DON '我的文件中有一個空行(兩個'\ n'一個接一個),如果我有,我的循環會中斷,文件讀取結束。如何從stdin中讀取文件,直到達到文件的<>重定向?
'file.write(sys.stdin.read())' – 2014-12-05 15:03:32