我在C:\Python27\Lib\site-packages\visual\examples
中有一个名为hsp.txt
的文本文件,并使用下面的代码。Python编码错误:“文件不存在”
def file():
file = open('hsp.txt', 'r')
col = []
data = file.readlines()
for i in range(1,len(data)-1):
col.append(int(float(data[i].split(',')[5])))
return col
def hist(col):
handspan = []
for i in range(11):
handspan.append(0)
for i in (col):
handspan[i] += 1
return handspan
col = file()
handspan = hist(col)
print(col)
print(handspan)
但是,当我运行它说,该文件不存在。
Traceback (most recent call last):
File "Untitled", line 17
col = file()
File "Untitled", line 2, in file
file = open('hsp.txt', 'r')
IOError: [Errno 2] No such file or directory: 'hsp.txt'
我该如何解决这个问题? 另外我如何输出均值和方差?
在“os.getcwd()”中输入。它返回什么? – Zizouz212
@MartijnPieters你是一个在这些可怕的问题上花费这么多时间的圣人。 – dbliss
@Jeffrey当你完成这个工作时,你应该真的关闭这个文件 - 或者用'with'代码块打开这个文件。 – dbliss