【发布时间】:2017-10-31 20:53:24
【问题描述】:
我正在尝试打开一个 csv 文件,以便将我的彩票号码(从 1 到 54 的六个随机生成的整数)与 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)
我不断收到错误:
with open('lottotexas.csv','r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'lottotexas.csv'.
我已经在我的计算机上下载了 csv 文件,但我不知道从那里去哪里,以便我的 python 程序可以找到 csv 文件。
【问题讨论】:
-
python 会尝试从当前工作目录打开文件,你必须提供文件的完整路径
-
换句话说,它可能会在您的脚本所在的同一目录中查找 CSV。如果您的 python 脚本不在该文件夹中,则将文件放在“下载”中将无济于事.如果它们位于不同的文件夹中,您需要提供文件的绝对路径或相对路径。
-
对于任何错误,您最好在问这里之前谷歌它。