試圖找出爲什麼這些意外的換行符正在打印在一個簡單的腳本中。我想在此腳本中執行的操作是在文件中讀取,並打印每行的內容。在Python中打印意外的換行符
的代碼是這樣的:
#!/usr/bin/python
import sys
# This script should just iterate over a file and echo the contents of each line
f=open(sys.argv[1],'r')
for line in f:
line.rstrip('\n')
print "Line 1 is: " + line
f.close()
輸入文件僅有2行:
#cat users
root
dontexist
#
當我運行這個輸入文件中的腳本,有輸出額外的新行:
#./test.py users
Line 1 is: root
Line 1 is: dontexist
#
我錯過了什麼?謝謝大家。