112 python file path relative-path
假设python代码在先前的Windows目录中称为"main"而不知道,并且在运行时安装代码的任何地方都需要访问目录"main/2091/data.txt".
我应该如何使用开放(位置)功能?应该是什么位置?
编辑:
我发现下面的简单代码会起作用..它有什么缺点吗?
file="\2091\sample.txt"
path=os.getcwd()+file
fp=open(path,'r+');
Run Code Online (Sandbox Code Playgroud)
Rus*_*uss 164
有了这种类型的东西,你需要小心你的实际工作目录.例如,您可能无法从文件所在的目录运行脚本.在这种情况下,您不能仅使用相对路径.
如果您确定所需的文件位于脚本实际所在的子目录下,您可以使用它__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)
Run Code Online (Sandbox Code Playgroud)
小智 29
这段代码工作正常:
import os
def readFile(filename):
filehandle = open(filename)
print filehandle.read()
filehandle.close()
fileDir = os.path.dirname(os.path.realpath('__file__'))
print fileDir
#For accessing the file in the same folder
filename = "same.txt"
readFile(filename)
#For accessing the file in a folder contained in the current folder
filename = os.path.join(fileDir, 'Folder1.1/same.txt')
readFile(filename)
#For accessing the file in the parent folder of the current folder
filename = os.path.join(fileDir, '../same.txt')
readFile(filename)
#For accessing the file inside a sibling folder.
filename = os.path.join(fileDir, '../Folder2/same.txt')
filename = os.path.abspath(os.path.realpath(filename))
print filename
readFile(filename)
Run Code Online (Sandbox Code Playgroud)
Gra*_*ard 21
我创建了一个帐户,所以我可以澄清我认为我在Russ的原始回复中发现的差异.
作为参考,他的原始答案是:
import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
Run Code Online (Sandbox Code Playgroud)
这是一个很好的答案,因为它试图动态创建所需文件的绝对系统路径.
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)
Run Code Online (Sandbox Code Playgroud)
生成的abs_file_path(在此示例中)变为: /path/to/dir/2091/data.txt
Wil*_*uck 17
这取决于您使用的操作系统.如果您想要一个兼容Windows和*nix的解决方案,例如:
from os import path
file_path = path.relpath("2091/data.txt")
with open(file_path) as f:
<do stuff>
Run Code Online (Sandbox Code Playgroud)
应该工作正常.
该path模块能够为其运行的任何操作系统格式化路径.此外,只要您具有正确的权限,python就可以正常处理相对路径.
编辑:
正如评论中提到的那样,python无论如何都可以在unix风格和windows风格的路径之间进行转换,所以即使是更简单的代码也可以工作:
with open("2091/data/txt") as f:
<do stuff>
Run Code Online (Sandbox Code Playgroud)
话虽这么说,该path模块仍然有一些有用的功能.
Âng*_*tto 11
我花了很多时间去发现为什么我的代码找不到在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')
Run Code Online (Sandbox Code Playgroud)
小智 9
尝试这个:
from pathlib import Path
data_folder = Path("/relative/path")
file_to_open = data_folder / "file.pdf"
f = open(file_to_open)
print(f.read())
Run Code Online (Sandbox Code Playgroud)
Python 3.4 引入了一个新的标准库,用于处理名为 pathlib 的文件和路径。这个对我有用!
小智 6
码:
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
Run Code Online (Sandbox Code Playgroud)
说明:
导入库:
import os
Run Code Online (Sandbox Code Playgroud)
用于__file__获取当前脚本的路径:
script_path = os.path.abspath(__file__)
Run Code Online (Sandbox Code Playgroud)
将脚本路径分为多个项目:
path_list = script_path.split(os.sep)
Run Code Online (Sandbox Code Playgroud)
删除列表中的最后一项(实际的脚本文件):
script_directory = path_list[0:len(path_list)-1]
Run Code Online (Sandbox Code Playgroud)
添加相对文件的路径:
rel_path = "main/2091/data.txt
Run Code Online (Sandbox Code Playgroud)
加入列表项,并添加相对路径的文件:
path = "/".join(script_directory) + "/" + rel_path
Run Code Online (Sandbox Code Playgroud)
现在,您可以设置要对文件执行的任何操作,例如:
file = open(path)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
376432 次 |
| 最近记录: |