我不断收到错误消息,该文件不存在。这是我第一次这样做,无法在google上找到足够的答案。
该文件存储在名称为“ CMPG 111”的文件夹中,文档名称为“ Data.txt”
阅读该内容的代码我是:
def read(data):
try:
count=0
INFILE=open("CMPG 111\\Data.txt","r")
for line in INFILE:
rawdata.append(line.rstrip())
count+=1
INFILE.close()
return count
except:
print("File could not be found")
return(count)
exit()
一种可靠的方法是列出文本文件的整个路径。
假设该文件夹位于桌面中,并且您的PC的用户名为User1:
def read(data):
try:
count=0
INFILE=open("C:\\Users\\User1\\UserCMPG 111\\Data.txt","r")
for line in INFILE:
rawdata.append(line.rstrip())
count+=1
INFILE.close()
return count
except:
print("File could not be found")
return(count)
exit()