从项目根目录获取文件路径。

huangapple go评论70阅读模式
英文:

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&#39;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始终不会改变,您可以通过\拆分路径,然后从列表的第三个元素开始连接:

&gt;&gt;&gt; path_to_file = &quot;/Users/UserName/project/first_level/second/somename/my_file.py&quot;
&gt;&gt;&gt; (&quot;/&quot;).join(path_to_file.split(&quot;/&quot;)[3:])
&#39;project/first_level/second/somename/my_file.py&#39;

如果您不知道它将位于何处,您可以在拆分的列表中查找项目,然后连接:

&gt;&gt;&gt; path_split = path_to_file.split(&quot;/&quot;)
&gt;&gt;&gt; (&quot;/&quot;).join(path_split[path_split.index(&quot;project&quot;)+1:])
&#39;first_level/second/somename/my_file.py&#39;
英文:

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:

&gt;&gt;&gt; path_to_file = &quot;/Users/UserName/project/first_level/second/somename/my_file.py&quot;
&gt;&gt;&gt; (&quot;/&quot;).join(path_to_file.split(&quot;/&quot;)[3:])
&#39;project/first_level/second/somename/my_file.py&#39;

If you don't know where it will be located, you can find project on the splited list and then join:

&gt;&gt;&gt; path_split = path_to_file.split(&quot;/&quot;)
&gt;&gt;&gt; (&quot;/&quot;).join(path_split[path_split.index(&quot;project&quot;)+1:])
&#39;first_level/second/somename/my_file.py&#39;

答案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

huangapple
  • 本文由 发表于 2023年6月5日 23:10:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定