我目前正在關注「瞭解Python的難題」。然而,當我使用.read()命令我的.txt文件它輸出在一個非常不可思議的方式文本,有額外的空間,並在啓動方:在Python中讀取txt文件
控制檯是Windows電源外殼。
我的代碼如下所示:
from sys import argv #imports argv from sys
script, filename = argv #unpacks script and filename from argv
txt = open(filename) #declares the variable txt as the text in filename
print "Here's your file %r" % filename #prints the string and the filename
print txt.read() #prints a reading of txt
txt.close()
print "Type the filename again:" #prints the string
file_again = raw_input("> ") #declares the variable file_again as the raw input
txt_again = open(file_again) #declares the variable txt_again as the text in file_again
print txt_again.read() #prints a reading of txt_again
txt.close()
而且文件看起來是這樣的:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
請幫幫忙!
在這裏沒有相同的行爲(debian linux),必須是你的文件或你的系統的東西。 – 2015-02-11 15:57:36