简体   繁体   English

FileNotFoundError: [Errno 2] 没有这样的文件或目录。 Python 无法正确读取文件

[英]FileNotFoundError: [Errno 2] No such file or directory. Python not reading files correctly

I am having some troubles when trying to open a file in the same directory.尝试打开同一目录中的文件时遇到一些麻烦。 When I open the file it brings me up this error:当我打开文件时,它给我带来了这个错误:

Traceback (most recent call last):
File "c:/Users/joseph/Desktop/direct path/test_path.py", line 3, in <module> 
with open("nba_reports.csv","r") as file:
FileNotFoundError: [Errno 2] No such file or directory: nba_reports.csv

When in reality that file named 'nba_reports.csv' is right next to where the python file is saved.实际上,名为“nba_reports.csv”的文件就在 python 文件的保存位置旁边。 I have had this problem in various different IDEs, I'm currently working on Visual Code and Visual Studio and I have managed to solve the problem by specifying the complete path from the file as "c:/Users/joseph/Desktop/direct path/nba_reports.csv".我在各种不同的 IDE 中都遇到过这个问题,我目前正在使用 Visual Code 和 Visual Studio,我已经通过将文件中的完整路径指定为“c:/Users/joseph/Desktop/direct path”来解决这个问题/nba_reports.csv”。 However, when I open this same code on Thonny it does recognize the file with no problem at all just by specifying the file name.但是,当我在 Thonny 上打开相同的代码时,它确实可以通过指定文件名来毫无问题地识别文件。 I would really appreciate if someone could help me out with this problem so that I can just write the file name on Visual Code.如果有人能帮助我解决这个问题,我将非常感激,这样我就可以在 Visual Code 上写下文件名。

The code is as followed:代码如下:

with open("nba_reports.csv","r") as file:
    for row in file:
        print(row)

You can use the os library to get the current working directory with您可以使用 os 库来获取当前工作目录

os.getcwd()

It's likely if you print the result of that, it will show Python isn't using the proper working directory.如果您打印结果,很可能会显示 Python 没有使用正确的工作目录。 To change it.改变它。 use os.chdir() as follows.使用 os.chdir() 如下。 The parameter you should give os.chdir follows the way you would use the cd command from the command line in a Unix based operating system.您应该给 os.chdir 的参数遵循您在基于 Unix 的操作系统中从命令行使用 cd 命令的方式。

>>> import os
>>> os.getcwd()
'/Users/myname'
>>> os.chdir('./Documents')
>>> os.getcwd()
'/Users/name/Documents'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileNotFoundError:[错误2]没有此类文件或目录。 的Ubuntu - FileNotFoundError: [Errno 2] No such file or directory. Ubuntu python:FileNotFoundError:[Errno 2]没有这样的文件或目录 - python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError:[错误2]没有这样的文件或目录 - Python FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录 [Python] - FileNotFoundError: [Errno 2] No such file or directory [Python] Python 3-FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3 - FileNotFoundError: [Errno 2] No such file or directory PyInstaller + UI 文件 - FileNotFoundError: [Errno 2] 没有这样的文件或目录: - PyInstaller + UI Files - FileNotFoundError: [Errno 2] No such file or directory: Pillow 不会在同一目录中打开图像。 “FileNotFoundError:[Errno 2] 没有这样的文件或目录:” - Pillow won't open an image in the same directory. “FileNotFoundError: [Errno 2] No such file or directory:” 如何修复“FileNotFoundError:没有这样的文件或目录。” 当使用 Python 时? - How to fix the "FileNotFoundError: No such file or directory." when using Python? FileNotFoundError: [Errno 2] 没有这样的文件或目录 Azure Python 错误 - FileNotFoundError: [Errno 2] No such file or directory Azure Python error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM