Python open() 需要完整路径
Python open() requires full path
我正在编写脚本来读取 csv 文件。 csv 文件和脚本位于同一目录中。但是当我试图打开文件时,它给了我 FileNotFoundError: [Errno 2] No such file or directory: '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)
open(file, mode='r', buffering=-1, encoding=None, errors=None,
newline=None, closefd=True, opener=None)
file is a path-like object giving the pathname (absolute or relative
to the current working directory) of the file to be opened or an
integer file descriptor of the file to be wrapped.
因此,如果您要打开的文件不在 运行 脚本的当前文件夹中,您可以使用绝对路径,或者获取工作目录 or/and 绝对路径使用:
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 Code,但如果给出准确的路径,我认为这仍然无关紧要。
我目前使用上述解决方案的代码:
Sitka_highs.py
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)
假设您在 main_folder
上并且想要调用文件 first_file.py
然后打开文件 readme.txt
:
main_folder
│
└───first_folder
│ │ first_file.py
│ │
| └───second_folder
│ │ readme.txt
│
└─── ...
Python 为我们提供了一个名为 __file__
的属性,即 returns 文件的绝对路径。例如,假设 first_file.py
的内容只是打印此路径的一行:print(__file__)
.
现在,如果我们从 main_folder
调用 first_file.py
,我们将得到与从 first_folder
调用它相同的结果(请注意,在 Windows 中,结果会有点不同):
"/Users/<username>/Documents/github/main_folder/first_folder/first_file.py"
而如果我们要获取first_file.py
的文件夹,我们导入库os
,我们使用方法os.path.dirname(__file__)
.
最后,我们将此文件夹与我们要从中访问的文件夹 (second_folder
) 和文件名 (readme.txt
) 连接起来。
化简,结果码为:
import os
DIR_ABS_PATH = os.path.dirname(__file__)
README_PATH = os.path.join(DIR_ABS_PATH, 'second_folder', 'readme.txt')
和README_PATH的值:
"/Users/<username>/Documents/github/main_folder/first_folder/second_folder/readme.txt"
这样,您也不会遇到与带有路径的 Visual Studio 代码调试器相关的任何问题,并且您可以从任何需要的地方调用 first_file.py
我正在编写脚本来读取 csv 文件。 csv 文件和脚本位于同一目录中。但是当我试图打开文件时,它给了我 FileNotFoundError: [Errno 2] No such file or directory: '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)
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped.
因此,如果您要打开的文件不在 运行 脚本的当前文件夹中,您可以使用绝对路径,或者获取工作目录 or/and 绝对路径使用:
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 Code,但如果给出准确的路径,我认为这仍然无关紧要。
我目前使用上述解决方案的代码:
Sitka_highs.py
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)
假设您在 main_folder
上并且想要调用文件 first_file.py
然后打开文件 readme.txt
:
main_folder
│
└───first_folder
│ │ first_file.py
│ │
| └───second_folder
│ │ readme.txt
│
└─── ...
Python 为我们提供了一个名为 __file__
的属性,即 returns 文件的绝对路径。例如,假设 first_file.py
的内容只是打印此路径的一行:print(__file__)
.
现在,如果我们从 main_folder
调用 first_file.py
,我们将得到与从 first_folder
调用它相同的结果(请注意,在 Windows 中,结果会有点不同):
"/Users/<username>/Documents/github/main_folder/first_folder/first_file.py"
而如果我们要获取first_file.py
的文件夹,我们导入库os
,我们使用方法os.path.dirname(__file__)
.
最后,我们将此文件夹与我们要从中访问的文件夹 (second_folder
) 和文件名 (readme.txt
) 连接起来。
化简,结果码为:
import os
DIR_ABS_PATH = os.path.dirname(__file__)
README_PATH = os.path.join(DIR_ABS_PATH, 'second_folder', 'readme.txt')
和README_PATH的值:
"/Users/<username>/Documents/github/main_folder/first_folder/second_folder/readme.txt"
这样,您也不会遇到与带有路径的 Visual Studio 代码调试器相关的任何问题,并且您可以从任何需要的地方调用 first_file.py