python如何获取当前工程根目录
大家讲道理
大家讲道理 2017-04-17 17:46:09
0
6
1554

假设工程根目录为 d:/Project/

然后在 d:/Project/a/b/c/d/file.py 里使用 os.getcwd() 方法获取的是
d:/Project/a/b/c/d

现在我想获取 d:/Project 怎么做?

------------------------------------分割线---------------------------------
谢谢大家的热情回答,都怪我没有将问题说清楚,我的意思是获取当前工程的根目录,而获取根目录的函数可能在任何目录下

根目录既可能是 d:/Project/
也可能是 d:/Python/Project/
也可能是 d:/balabala/Python/Project/
因此逐级往上也是不可能找到的,我目前的方法是在根目录下放一个文件 file

然后这样:

def getSeparator():
    if 'Windows' in platform.system():
        separator = '\\'
    else:
        separator = '/'
    return separator

def findPath(file):
    o_path = os.getcwd()
    separator = getSeparator()
    str = o_path
    str = str.split(separator)
    while len(str) > 0:
        spath = separator.join(str)+separator+file
        leng = len(str)
        if os.path.exists(spath):
            return spath
        str.remove(str[leng-1])

但是这样也有一个问题,就是必须保证任何目录下不能有和根目录下file重名的文件

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(6)
黄舟

There is no good way unless your Project directory is never moved.

Add a module in the Project directory to get the path of the current fileproject_dir = os.path.dirname(os.path.abspath(__file__)), ​​and then import it in file.py

小葫芦

Go up step by step
os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd()))))

阿神

If you finally mobilize file.py's os.getcwd() under the Project file, you will get the result of D:/Project. os.getcwd()的话,就会得到D:/Project这个结果。

比如说你建立了一个这样的项目结构,如果在是在D:/Project/a/b/c/d/file.py直接调用的话,很自然就是得file.py文件所在的工作目录的路径。

如果是在D:/Project/__init__.py调用呢,那就是得到D:/Project/__init__.py所在的工作目录的路径,也就是D:/Project/了。

os.getcwd()

For example, if you create a project structure like this, if you call it directly in D:/Project/a/b/c/d/file.py, it will naturally be file.pyThe path to the working directory where the file is located.

If it is called from D:/Project/__init__.py, then the path to the working directory where D:/Project/__init__.py is located is obtained, that is, D:/Project/.

🎜os.getcwd()The results obtained are different depending on the path of the calling file. 🎜 🎜 🎜If you are sure of the name of the project directory and ensure that no folder with the same name will appear in the project directory, you can use regular expressions. 🎜
import re, os

# 测试
paths = ['d:\Project\', 'home/Python/Project/', 'c:/balabala/Python/Project/']
for path in paths:
    pj_dir = re.match('.*Project', path)
    print(pj_dir.group())
    
# 在子文件下就应该这样用
print(re.match('(.*\{sep}Project)\{sep}'.format(sep=os.sep), __file__).group(1))
小葫芦
import sys,os
path = sys.path[0]
阿神
 aList = os.getcwd().split('\\')
 path = aList[0]+'\\'+aList[1]
小葫芦
import os

s1 = "d:/Project/a/b/c/d/file.py"
s2 = r"d:\project\a\b\c\d\file.py"

for i in [s1, s2]:
    abs_path = os.path.abspath(i).split(os.sep)
    print os.path.abspath(abs_path[0] + os.sep + abs_path[1])

Directory separator for "" and "/"

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template