我目前正在关注“了解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