我试着打开一个csv文件,这样我就可以将我的彩票号码(从1到54随机生成的6个整数)与1992年至2017年的中奖号码进行比较,这也是csv文件所包含的内容。在
import random
import csv
six_random_int =[random.randint(1,54),random.randint(1,54),random.randint(1,54),random.randint(1,54),random.randint(1,54),random.randint(1,54)]
print('Your lotto ticket is ',six_random_int)
with open('lottotexas.csv','r') as f: #This is where the error is occurring
reader = csv.reader(f)
for row in reader:
print(row)
我一直收到错误:
^{pr2}$我把csv文件下载到我的电脑上,但我不知道从那里到哪里,这样我的python程序就可以找到csv文件。在
请将文件复制并粘贴到python程序所在的同一路径(folder/directory/place)。 再次运行你的程序。 如果问题仍然存在,请检查文件权限。在
请尝试指定文件的完整路径。必须让python在文件所在的同一文件夹中执行。在
从
error
中可以很清楚地看到:Python找不到文件
'lottotexas.csv'
。在Python尝试},除非您已经给出了一个完整的}。在
open
文件的方法是在.py
file
的当前directory
中查找{file path
。如果给file
一个完整的path
,那么Python将从该位置打开{尽管如此,按照您编写这个程序的方式,Python试图在与Python}来命名它!-因此
file
相同的file
中找到一个名为:lottotexas.csv
的file
,而且没有{error
:FileNotFoundError
!在相关问题 更多 >
编程相关推荐