在python中读取csv文件时出现错误“没有这样的文件或目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18062759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:50:14  来源:igfitidea点击:

error "no such file or directory" when reading in csv file in python

pythoncsvfilepath

提问by erik.garcia294

Currently I am trying to read in a csv file using the csv module in python. When I run the piece of code below I get an error that states the file does not exist. My first guess is that maybe, I have the file saved in the wrong place or I need to provide pyton with a file path. currently I have the file saved in C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv.

目前我正在尝试使用 python 中的 csv 模块读取 csv 文件。当我运行下面的代码时,我收到一个错误,指出该文件不存在。我的第一个猜测是,也许我将文件保存在错误的位置,或者我需要为 pyton 提供文件路径。目前我将文件保存在 C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv。

one side note im note sure wether having set the mode to 'rb' (read binary) was the right move.

一个旁注我注意到确定将模式设置为“rb”(读取二进制)是正确的举动。

import csv
with open('test_satdata.csv', 'rb') as csvfile:
    satreader = csv.reader(csvfile, delimiter=' ', lineterminator=" ")
    for row in satreader:
        print ', '.join(row)

Here is the errror code.

这是错误代码。

Traceback (most recent call last):
File "C:/Python27/test code/test csv parse.py", line 2, in <module>
    with open('test_satdata.csv', 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'test_satdata.csv'

采纳答案by Martijn Pieters

Your code is using a relative path; python is looking in the current directory (whatever that may be) to load your file. What the current directory isdepends on how you started your Python script and if you executed any code that may have changed the current working directory.

您的代码使用的是相对路径;python 正在查看当前目录(无论是什么)以加载您的文件。当前目录什么取决于您如何启动 Python 脚本以及您是否执行了任何可能更改了当前工作目录的代码。

Use a full absolute path instead:

改用完整的绝对路径:

path = r'C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv'
with open(path, 'rb') as csvfile:

Using 'rb'is entirely correct, the csvmodulerecommends you do so:

使用'rb'是完全正确的,csv模块建议您这样做:

If csvfileis a file object, it must be opened with the ‘b' flag on platforms where that makes a difference.

如果csvfile是文件对象,则必须在有区别的平台上使用 'b' 标志打开它。

Windows issuch a platform.

Windows就是这样一个平台。

回答by Amit

Your current guess is right: either put the file in your test code directory or point python to the right path.

您当前的猜测是正确的:要么将文件放在您的测试代码目录中,要么将 python 指向正确的路径。

回答by Ioannis Chrysochos

Make a fresh rename of your folder. That worked for me.

重新命名您的文件夹。那对我有用。

回答by Jeremy Thompson

You can hit this error when you're running a Python script from a Directory where the file is not contained.

当您从不包含文件的目录中运行 Python 脚本时,您可能会遇到此错误。

Sounds simple to fix, put the CSV file in the same folder as the .PY file. However when you're running under an IDE like VSCode the command output might cdto another directory when it executes your python file.

听起来很容易修复,将 CSV 文件与 .PY 文件放在同一文件夹中。但是,当您在 VSCode 等 IDE 下运行时,命令输出可能会cd在执行您的 python 文件时转到另一个目录。

PS C:\git\awesome> cd 'c:\git\awesome'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; 
& 'C:\Program Files (x86)\Python37-32\python.exe' 'c:\Users\jeremy\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\ptvsd_launcher.py' 
'--default' '--client' '--host' 'localhost' '--port' '1089' 
'c:\git\Project\ReadCSV.py'

See how I'm in my awesome repo and the first command is: cd 'c:\git\awesome';

看看我在我很棒的 repo 中的状态,第一个命令是: cd 'c:\git\awesome';

Then it executes the python file: 'c:\git\Project\ReadCSV.py'

然后它执行python文件:'c:\git\Project\ReadCSV.py'

So its expecting the CSV file in 'c:\git\awesome'.

所以它期待 'c:\git\awesome' 中的 CSV 文件。

To fix it, either use the full file names or CD to the directory containing the CSV file you wish to read.

要修复它,请使用完整文件名或 CD 到包含您要读取的 CSV 文件的目录。