假设我的Python代码在名为main
的目录中执行,应用程序需要访问main/2091/data.txt
。
我应该如何使用 open(location)
? 参数location
应该是什么?
我发现下面这个简单的代码可以工作...它有什么缺点吗?
file = "\2091\sample.txt"
path = os.getcwd()+file
fp = open(path, 'r+');
假设我的Python代码在名为main
的目录中执行,应用程序需要访问main/2091/data.txt
。
我应该如何使用 open(location)
? 参数location
应该是什么?
我发现下面这个简单的代码可以工作...它有什么缺点吗?
file = "\2091\sample.txt"
path = os.getcwd()+file
fp = open(path, 'r+');
在这种情况下,你需要注意你实际的工作目录是什么。例如,你可能没有从文件所在的目录运行脚本。在这种情况下,你不能仅使用相对路径。
如果你确定想要的文件在脚本实际位于的子目录中,你可以使用 __file__
来帮助解决问题。__file__
是正在运行的脚本所在位置的完整路径。
因此,你可以使用类似以下代码:
import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
__file__
是一个相对路径(至少在我的设置中是这样,出于某种原因),你需要先调用 os.path.abspath(__file__)
。 osx/homebrew 2.7 - Cory MawhorterNameError:name '__file __'未定义
。 - Soumendraos.path.join
来处理2091/data.txt
吗?在Windows系统中,使用\\
和/
会导致问题,不是吗? - FLAW这段代码运行良好:
import os
def read_file(file_name):
file_handle = open(file_name)
print file_handle.read()
file_handle.close()
file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir
#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)
#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)
#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)
#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
'__file__'
中去掉引号,这样就变成了:fileDir = os.path.dirname(os.path.realpath(__file__))
。 - pchtspos.path.join(path,'folderA','file.txt')
。 - Mauricio Gracia Gutierrez__file__
不是 '__file__'
。 - Marius我创建了一个账户,只是为了澄清我认为在Russ原始回答中发现的差异。
作为参考,他原始回答如下:
import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
这是一个很好的答案,因为它试图动态创建所需文件的绝对系统路径。
Cory Mawhorter注意到__file__
是一个相对路径(在我的系统上也是如此),并建议使用os.path.abspath(__file__)
。 然而,os.path.abspath
返回的是你当前脚本的绝对路径(即/path/to/dir/foobar.py
)
要使用这种方法(以及我最终让它工作的方式),您必须从路径末尾删除脚本名称:
import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
在这个例子中,最终的abs_file_path为:/path/to/dir/2091/data.txt
os.path.dirname(os.path.abspath(__file__))
。 - Luke Tayloros.path.dirname
的功能要好,就像我去年在回答中所做的那样。 - Grant Hulegaard这取决于你使用的操作系统。如果你想要一个适用于Windows和*nix的解决方案,可以尝试以下方式:
from os import path
file_path = path.relpath("2091/data.txt")
with open(file_path) as f:
<do stuff>
应该可以正常工作。
path
模块能够格式化适用于当前操作系统的路径。此外,只要有正确的权限,Python 就可以处理相对路径。
编辑:
如评论中kindall所提到的那样,Python 可以在unix风格和windows风格之间进行转换,因此甚至更简单的代码也可以正常工作:
with open("2091/data/txt") as f:
<do stuff>
话虽如此,path
模块仍具有一些有用的函数。
relpath()
函数将一个路径名转换为相对路径。由于它已经是一个相对路径,所以它不会做任何事情。 - kindallos.path
模块中是否有另一个更好的选择? - Wilduck__file__
。 请参阅我的答案。 - Russ我花了很多时间在Windows系统上运行Python 3时,发现我的代码无法找到文件的原因。所以我在斜杠前面加了一个点,然后一切都正常了:
import os
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, './output03.txt')
print(file_path)
fptr = open(file_path, 'w')
file_path = os.path.join(script_dir, 'output03.txt')
- Mr_and_Mrs_Dscript_dir = os.path.abspath(os.path.dirname(__file__))
。 - Mr_and_Mrs_D试试这个:
from pathlib import Path
data_folder = Path("/relative/path")
file_to_open = data_folder / "file.pdf"
f = open(file_to_open)
print(f.read())
Python 3.4 引入了一个处理文件和路径的新标准库,称为 pathlib。对我来说很有用!
with data_folder.open() as f
替代原有的方法。 - Neuron代码:
import os
script_path = os.path.abspath(__file__)
path_list = script_path.split(os.sep)
script_directory = path_list[0:len(path_list)-1]
rel_path = "main/2091/data.txt"
path = "/".join(script_directory) + "/" + rel_path
解释:
导入库:
import os
使用__file__
获取当前脚本的路径:
script_path = os.path.abspath(__file__)
将脚本路径分成多个项目:
path_list = script_path.split(os.sep)
script_directory = path_list[0:len(path_list)-1]
添加相关文件的路径:
rel_path = "main/2091/data.txt
将列表项组合起来,并添加相对路径下的文件:
path = "/".join(script_directory) + "/" + rel_path
file = open(path)
path = "/".join(script_directory) + "/" + rel_path
,例如path = os.path.join(script_directory, rel_path)
。你应该使用script_path = os.path.dirname(__file__)
来代替手动解析路径。 - Mr_and_Mrs_Dimport os
def file_path(relative_path):
dir = os.path.dirname(os.path.abspath(__file__))
split_path = relative_path.split("/")
new_path = os.path.join(dir, *split_path)
return new_path
with open(file_path("2091/data.txt"), "w") as f:
f.write("Powerful you have become.")
os.join
将您的相对文件添加到结尾。# get parent folder with `os.path`
import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# now use BASE_DIR to get a file relative to the current script
os.path.join(BASE_DIR, "config.yaml")
pathlib
实现:# get parent folder with `pathlib`'s Path
from pathlib import Path
BASE_DIR = Path(__file__).absolute().parent
# now use BASE_DIR to get a file relative to the current script
BASE_DIR / "config.yaml"
open('../follower.txt', 'r').read()
来读取该文件。
r"\2091\sample.txt"
这样使用原始字符串。或者转义它们,如"\\2091\\sample.txt"
(但这很烦人)。还有2)您正在使用getcwd(),它是在执行脚本时所在的路径。我以为您想要相对于脚本位置(但现在不确定了)。3)始终使用os.path函数操作路径。您的路径拼接行应该是os.path.join(os.getcwd(), file)
。4)分号是没有意义的。 - Russwith open(path, 'r+') as fp:
。点击这里查看我看过的最好的with
语句解释。 - Russos.path.abspath
可以轻松获取相对路径打开的完整路径。最终语句看起来像这样:os.path.abspath('./2091/sample.txt')
。 - OPMendeavor