英文:
How can I get a file path from project root
问题
这是我的完整文件路径
/Users/UserName/project/first_level/second/somename/my_file.py
我导入该文件,然后想要获取它相对于项目的路径(对于我来说,根目录是 project
)
我需要获取 first_level/second/somename/my_file.py
当我执行 my_file.__file__
时,我得到完整路径
有人可以帮忙吗?
英文:
This is my full file path
/Users/UserName/project/first_level/second/somename/my_file.py
I import the file and then want to get it path from relative to project(for me the root is project
)
I need to get the first_level/second/somename/my_file.py
When I do my_file.__file__
I get the full path
Can someone help?
答案1
得分: 1
可以使用 os.path.relpath
。
假设你有以下目录结构:
project
│ main.py
|
└───first_level
│ │
| |____second_level
| | my_file.py
| ...
如果你运行 main.py:
import os
from pathlib import Path
import first_level.second_level.my_file
# 将会打印从 main.py 到 my_file.py 文件的相对路径
print(os.path.relpath(my_file.__file__, Path(__file__).parent))
如果你想从另一个文件获取到项目根目录的相对路径,你需要在某处设置 ROOT_DIR
,然后运行 print(os.path.relpath(my_file.__file__, ROOT_DIR))
。
(在 Django 中的 settings.py 中通常会设置类似于以下的 BASE_DIR
:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
```)
<details>
<summary>英文:</summary>
You can use `os.path.relpath`
Let's say that you have structure
project
│ main.py
|
└───fist_level
│ │
| |____second_level
| | my_file.py
| ...
If you run main.py
```py
import os
from pathlib import Path
import fist_level.second_level.my_file
# will print relative path to my_file.py file from main.py
print(os.path.relpath(my_file.__file__, Path(__file__).parent))
If you want to get the relative path to your root project from another file you will need to set somewhere
ROOT_DIR
and then run print(os.path.relpath(my_file.__file__, ROOT_DIR))
(Very similar in Django in settings.py it usually sets
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
答案2
得分: 0
如果您确定/Users/UserName/project
始终不会改变,您可以通过\
拆分路径,然后从列表的第三个元素开始连接:
>>> path_to_file = "/Users/UserName/project/first_level/second/somename/my_file.py"
>>> ("/").join(path_to_file.split("/")[3:])
'project/first_level/second/somename/my_file.py'
如果您不知道它将位于何处,您可以在拆分的列表中查找项目,然后连接:
>>> path_split = path_to_file.split("/")
>>> ("/").join(path_split[path_split.index("project")+1:])
'first_level/second/somename/my_file.py'
英文:
If you are sure that /Users/UserName/project
will be always the same you can split the path by \
and then join from the third element in the list to the end:
>>> path_to_file = "/Users/UserName/project/first_level/second/somename/my_file.py"
>>> ("/").join(path_to_file.split("/")[3:])
'project/first_level/second/somename/my_file.py'
If you don't know where it will be located, you can find project on the splited list and then join:
>>> path_split = path_to_file.split("/")
>>> ("/").join(path_split[path_split.index("project")+1:])
'first_level/second/somename/my_file.py'
答案3
得分: 0
From my_file.py
one can use relpath()
.
my_file.py
import os
print(os.path.relpath(__file__, os.curdir))
Should give you:
first_level/second/somename/my_file.py
When imported
英文:
From my_file.py
one can use relpath()
.
my_file.py
import os
print(os.path.relpath(__file__, os.curdir))
Should give you :
first_level/second/somename/my_file.py
When imported
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论