Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python open()需要完整路径_Python_File_Visual Studio Code_Python 3.4 - Fatal编程技术网

Python open()需要完整路径

Python open()需要完整路径,python,file,visual-studio-code,python-3.4,Python,File,Visual Studio Code,Python 3.4,我正在编写一个脚本来读取csv文件。csv文件和脚本位于同一目录中。但是当我试图打开文件时,它给了我FileNotFoundError:[Errno 2]没有这样的文件或目录:“zipcodes.csv”。我用来读取文件的代码是 with open('zipcodes.csv', 'r') as zipcode_file: reader = csv.DictReader(zipcode_file) 如果我给出文件的完整路径,它就会工作。为什么open()需要文件的完整路径?我认为Pyt

我正在编写一个脚本来读取csv文件。csv文件和脚本位于同一目录中。但是当我试图打开文件时,它给了我
FileNotFoundError:[Errno 2]没有这样的文件或目录:“zipcodes.csv”
。我用来读取文件的代码是

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

如果我给出文件的完整路径,它就会工作。为什么
open()
需要文件的完整路径?

我认为Python不知道使用哪个目录。。。要从当前python.py文件的当前路径开始,请尝试:

mypath = os.path.dirname(os.path.abspath(__file__))
with open(mypath+'/zipcodes.csv', 'r') as zipcode_file:
    reader = csv.DictReader(zipcode_file)
从:

打开(文件,mode='r',buffering=-1,encoding=None,errors=None, 换行符=None,closefd=True,opener=None)

文件是提供路径名(绝对或相对)的类似路径的对象 要打开的文件的当前工作目录)或 要包装的文件的整数文件描述符

因此,如果要打开的文件不在正在运行的脚本的当前文件夹中,则可以使用绝对路径,或者使用以下方法获取工作目录或/和绝对路径:

import os
# Look to the path of your current working directory
working_directory = os.getcwd()
# Or: file_path = os.path.join(working_directory, 'my_file.py')
file_path = working_directory + 'my_file.py'
或者,您可以在运行脚本时使用以下命令检索绝对路径:

import os
# Look for your absolute directory path
absolute_path = os.path.dirname(os.path.abspath(__file__))
# Or: file_path = os.path.join(absolute_path, 'folder', 'my_file.py')
file_path = absolute_path + '/folder/my_file.py'
如果您想成为操作系统无关者,则可以使用:

file_path = os.path.join(absolute_path, folder, my_file.py)

我已经确定了问题所在。我在Visual Studio代码调试器上运行代码。我打开的根目录高于我的文件级别。当我打开同一个目录时,它工作正常。

我使用了下面的方法,它对我来说工作正常

FILE_PATH = os.path.dirname(os.path.realpath(__file__))
config = ConfigParser.ConfigParser()
config.readfp(open(FILE_PATH+'/conf.properties'))

如果我不使用绝对路径或使用os.path构建路径,那么通过Python打开文件会遇到相当大的问题。即使该文件与Python文件位于同一目录中,结果也是一样的。我使用了Chiheb的解决方案,当然它又起作用了(谢谢Chiheb)。我确实想知道这是否与Python有关。我使用的是VS代码,但在我看来,如果给出了一个准确的路径,那么这并不重要

使用上述解决方案工作的我当前情况的代码:

锡特卡大学高中

import os
import csv

absolute_path = os.path.dirname(os.path.abspath(__file__))

filename = absolute_path + '/data/sitka_weather_07-2018_simple.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)

什么是
os.getcwd()
?如果该目录与包含该文件的目录不相同,则是相对路径不起作用的原因。因为您在其他目录中运行python脚本,而该目录是您的当前目录。我正在Visual Studio代码调试器中运行代码…其当前工作目录是什么?我得到了相同的结果。当我在
VS code
中运行脚本时,它是正常的(因为它识别当前工作目录),但当在
windows10
上从
c:
驱动器运行单独的命令提示符时,除非我导航到同一当前工作目录,否则代码将失败。一种解决方案是使用上述绝对路径或正则表达式
r“path\u to\u file\file\u name.csv”