我正在构建一个简单的助手脚本,用于将代码库中的两个模板文件复制到当前目录。但是,我没有存储模板的目录的绝对路径。我有一个相对路径从脚本,但当我调用脚本,它把它作为一个相对于当前工作目录的路径。是否有一种方法来指定这个相对url是来自脚本的位置?


当前回答

这是向系统路径集添加相对路径的简单方法。例如,对于目标目录比工作目录高一级(例如'/../')的常见情况:

import os
import sys
workingDir = os.getcwd()
targetDir = os.path.join(os.path.relpath(workingDir + '/../'),'target_directory')
sys.path.insert(0,targetDir)

对该解决方案进行了测试:

Python 3.9.6 |由conda-forge |打包(默认,2021年7月11日, 03:37:25) [MSC .1916 64位(AMD64)]

其他回答

Hi首先你需要理解os。path。abspath(path)和os。path。relpath(path)

简而言之,os.path.abspath(path)将相对路径转换为绝对路径。如果提供的路径本身是一个绝对路径,那么函数返回相同的路径。

类似地,os.path.relpath(path)将绝对路径转换为相对路径。如果提供的路径本身是相对路径,则函数返回相同的路径。

下面的例子可以让你正确理解上面的概念:

假设我有一个文件input_file_list.txt,其中包含要由我的python脚本处理的输入文件列表。

D: \ \ input1.dic浓缩

D: \ \ input2.dic浓缩

D: \ Copyioconc input_file_list。txt

如果你看到上面的文件夹结构,input_file_list.txt在Copyofconc文件夹中,python脚本要处理的文件在conc文件夹中

但是input_file_list.txt文件的内容如下所示:

. . \ \ input1.dic浓缩

. . \ \ input2.dic浓缩

我的python脚本在D: drive中。

input_file_list.txt文件中提供的相对路径是相对于input_file_list.txt文件的路径。

因此,当python脚本执行当前工作目录时(使用os.getcwd()获取路径)

由于我的相对路径是相对于input_file_list.txt,即“D:\Copyofconc”,我必须将当前工作目录更改为“D:\Copyofconc”。

所以我必须使用os.chdir('D:\Copyofconc'),所以当前的工作目录应该是“D:\Copyofconc”。

现在获取文件input1。Dic和input2。Dic,我将读取“..\conc\input1”这行。然后使用命令

Input1_path = os.path.abspath('..\conc\input1.dic')(将相对路径更改为绝对路径。这里作为当前的工作目录是“D:\Copyofconc”,文件“.\conc\input1.”相对于"D:\Copyofconc")

所以input1_path应该是“D:\conc\input1.dic”

而不是使用

import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'relative/path/to/file/you/want')

在公认的答案中,使用它会更健壮:

import inspect
import os
dirname = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
filename = os.path.join(dirname, 'relative/path/to/file/you/want')

因为使用__file__将返回加载模块的文件,如果它是从一个文件中加载的,所以如果从其他地方调用带有脚本的文件,则返回的目录将不正确。

这些答案提供了更多的细节:https://stackoverflow.com/a/31867043/5542253和https://stackoverflow.com/a/50502/5542253

例子


下面是一个用Python '3.9.5 '测试的例子:

您的当前目录:'c:\project1\code\' 你想要访问以下文件夹:'c:\project1\dataset\train\'。 然后您可以使用以下地址访问该文件夹:“../dataset/train/”

参考文献


如果你想了解更多关于Python中路径的信息,请阅读以下内容:

Pep - 355 Pep - 519

看到sys.path 在程序启动时初始化后,该列表的第一项路径[0]是包含用于调用Python解释器的脚本的目录。

使用此路径作为根文件夹,从中应用相对路径

>>> import sys
>>> import os.path
>>> sys.path[0]
'C:\\Python25\\Lib\\idlelib'
>>> os.path.relpath(sys.path[0], "path_to_libs") # if you have python 2.6
>>> os.path.join(sys.path[0], "path_to_libs")
'C:\\Python25\\Lib\\idlelib\\path_to_libs'

考虑一下我的代码:

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)