为什么我在阅读csv文件时出错?

时间:2017-10-31 20:53:24

标签: python csv pycharm

我试图打开一个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文件。

3 个答案:

答案 0 :(得分:0)

尝试指定文件的完整路径。您必须在放置文件的同一文件夹中执行python。

答案 1 :(得分:0)

请将文件复制并粘贴到您拥有python程序的相同路径(文件夹/目录/地点)中。 并再次运行您的程序。 如果问题仍然存在,请检查文件权限。

答案 2 :(得分:0)

error可以清楚地了解到:

with open('lottotexas.csv','r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'lottotexas.csv'. 

Python无法找到文件'lottotexas.csv'

Python尝试open该文件的方式是查看directory的{​​{1}} .py的当前file - 除非您给了一个完整的filename。如果您向file path提供完整的path,则Python会从该位置打开file

尽管如此,你编写这个程序的方式,Python正在尝试在与file相同的file中找到名为lottotexas.csv的{​​{1}} - 并且没有directory命名! - 因此filefile

相关问题