python中的相对地址
关于这里的问题。我也有类似的问题 我的项目结构:python中的相对地址,python,python-3.x,Python,Python 3.x,关于这里的问题。我也有类似的问题 我的项目结构: proj | |--config | |--ENV | |--folder1 | |--UI | |--test2.py | |--test1.py 我想使用test1.py和test2.py访问ENV文件,但我要使用相对地址,这样我就不必每次移动项目时都更改代码 import inspect import os dirname = os.path.dirname(os.path.abspath(in
proj
|
|--config
| |--ENV
|
|--folder1
| |--UI
| |--test2.py
|
|--test1.py
我想使用test1.py和test2.py访问ENV文件,但我要使用相对地址,这样我就不必每次移动项目时都更改代码
import inspect
import os
dirname = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
filename = os.path.join(dirname, 'config/ENV')
上面的代码在test1.py上运行良好,但在test2.py上失败,因为现在dirname
已更改
我正在寻找我可以在这两个文件上使用的代码。目前我只有一个想法,就是根据\
查找proj
并追加config/ENV
有没有更好更干净的方法
C:\Users\User1\proj\config/ENV -- at test1.py
C:\Users\User1\proj\folder1\UI\config/ENV -- at test2.py
我当前的解决方案:
dirname = os.path.dirname(__file__)
PROJECT_NAME = "proj"
x = dirname[:dirname.find(PROJECT_NAME) + len(PROJECT_NAME)]
filename = os.path.join(x, 'config/ENV')
如果要从某个_file.py访问资源,相对路径将是/resources/…
,但如果要使用另一个_file.py中的资源,则可以执行。/resources/…
因此,在您的例子中,如果您想从test1.py访问ENV文件,它的相对位置将是/config/ENV
,但是如果您想从test2.py访问它,它的相对位置将是。/../config/ENV
记住。/
表示上升一级,/
表示同一级
编辑:这里是固定路径
config/ENV
。在relative_path()
中传递该固定路径将为您提供相对路径地址
# proj
# config
# ENV
#
# folder1
# config
# some_other_file.txt
#
# UI
# test2.py
#
# test1.py
import os
def relative_path(path):
# Get the parent directory of the
# file that you need the relative
# path for.
my_dir = path.split('/')[0]
# Recursively match the directory
# in the given path. if the match
# not found go up one level.
def match_dir(c_path):
c_path = os.path.split(c_path)[0]
if my_dir in os.listdir(c_path) and (
os.path.isfile(
os.path.join(c_path, path)
)
):
# this whole if-block can be omitted
# if the folder you're trying to access
# is on the same level, this just prepends
# with './'. I added this just for
# aesthetic reason.
if os.path.basename(__file__) in os.listdir(c_path):
return './' + path
return path
return "../" + match_dir(c_path)
return match_dir(
os.path.realpath(__file__)
)
# Getting relative path from test2.py
print(relative_path("config/ENV"))
print(relative_path("config/some_other_file.txt"))
# Try running this from test1.py
print(relative_path("config/ENV")) # './config/ENV'
这不是很优化。只是一个大概的想法。是的,我知道,但我想在文件test1和test2上运行相同的代码。实际上,我的项目中有多个入口点,ENV需要读取一次,因此我正在寻找可以工作的代码,无论主文件或起始文件是什么,如果您想从这两个文件中获得相同的相对地址,那我认为这是不可能的。test2.py是两层的。也许您可以在从test2.py访问ENV文件时动态地预先添加
。/../
。是的,我认为每次都必须预处理基地址。@anoop抱歉,我知道太晚了。我有工作要做。所以我编辑了我的答案。让我知道这是否有帮助。@spectras link建议使用不同的路径(使用../)。在我的情况下,每次尝试从不同的入口点访问ENV文件时,我都必须更改代码,因为我的项目有多个入口点。您可以使用\uu file\uuuu
技术获得项目根目录的完整路径。从那里你可以添加你的路径,总是一样的。
# proj
# config
# ENV
#
# folder1
# config
# some_other_file.txt
#
# UI
# test2.py
#
# test1.py
import os
def relative_path(path):
# Get the parent directory of the
# file that you need the relative
# path for.
my_dir = path.split('/')[0]
# Recursively match the directory
# in the given path. if the match
# not found go up one level.
def match_dir(c_path):
c_path = os.path.split(c_path)[0]
if my_dir in os.listdir(c_path) and (
os.path.isfile(
os.path.join(c_path, path)
)
):
# this whole if-block can be omitted
# if the folder you're trying to access
# is on the same level, this just prepends
# with './'. I added this just for
# aesthetic reason.
if os.path.basename(__file__) in os.listdir(c_path):
return './' + path
return path
return "../" + match_dir(c_path)
return match_dir(
os.path.realpath(__file__)
)
# Getting relative path from test2.py
print(relative_path("config/ENV"))
print(relative_path("config/some_other_file.txt"))
# Try running this from test1.py
print(relative_path("config/ENV")) # './config/ENV'