Python open()需要完整路径 [英] Python open() requires full path

查看:113
本文介绍了Python open()需要完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来读取一个csv文件. csv文件和脚本位于同一目录中.但是,当我尝试打开文件时,它会给我FileNotFoundError: [Errno 2] No such file or directory: 'zipcodes.csv'.我用来读取文件的代码是

I am writing a script to read a csv file. The csv file and script lies in the same directory. But when I tried to open the file it gives me FileNotFoundError: [Errno 2] No such file or directory: 'zipcodes.csv'. The code I used to read the file is

with open('zipcodes.csv', 'r') as zipcode_file:
    reader = csv.DictReader(zipcode_file)

如果我提供文件的完整路径,它将起作用.为什么open()需要文件的完整路径?

If I give the full path to the file, it will work. Why open() requires full path of the file ?

推荐答案

我已经确定了问题所在.我在Visual Studio代码调试器上运行代码.我打开的根目录高于文件级别.当我打开相同的目录时,它起作用了.

I have identified the problem. I was running my code on Visual Studio Code debugger. The root directory I have opened was above the level of my file. When I opened the same directory, it worked.

这篇关于Python open()需要完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆