簡體   English   中英

從 Python 腳本獲取當前目錄的父目錄

[英]Get parent of current directory from Python script

我想從 Python 腳本中獲取當前目錄的父目錄。 例如,我從/home/kristina/desire-directory/scripts啟動腳本,在這種情況下,期望路徑是/home/kristina/desire-directory

我從sys知道sys.path[0] 但我不想解析sys.path[0]結果字符串。 有沒有其他方法可以在 Python 中獲取當前目錄的父目錄?

使用 os.path

獲取包含腳本的目錄的父目錄(無論當前工作目錄如何),您需要使用__file__

腳本內部使用os.path.abspath(__file__)獲取腳本的絕對路徑,調用os.path.dirname兩次:

from os.path import dirname, abspath
d = dirname(dirname(abspath(__file__))) # /home/kristina/desire-directory

基本上,您可以根據需要多次調用os.path.dirname來遍歷目錄樹。 例子:

In [4]: from os.path import dirname

In [5]: dirname('/home/kristina/desire-directory/scripts/script.py')
Out[5]: '/home/kristina/desire-directory/scripts'

In [6]: dirname(dirname('/home/kristina/desire-directory/scripts/script.py'))
Out[6]: '/home/kristina/desire-directory'

如果要獲取當前工作目錄的父目錄,請使用os.getcwd

import os
d = os.path.dirname(os.getcwd())

使用路徑庫

您還可以使用pathlib模塊(在 Python 3.4 或更高版本中可用)。

每個pathlib.Path實例都有指向父目錄的parent屬性,以及parents屬性,它是路徑的祖先列表。 Path.resolve可用於獲取絕對路徑。 它還解析所有符號鏈接,但如果這不是所需的行為,您可以使用Path.absolute代替。

Path(__file__)Path()分別代表腳本路徑和當前工作目錄,因此為了獲取腳本目錄的父目錄(無論當前工作目錄如何),您將使用

from pathlib import Path
# `path.parents[1]` is the same as `path.parent.parent`
d = Path(__file__).resolve().parents[1] # Path('/home/kristina/desire-directory')

獲取當前工作目錄的父目錄

from pathlib import Path
d = Path().resolve().parent

請注意, d是一個Path實例,它並不總是很方便。 您可以在需要時輕松將其轉換為str

In [15]: str(d)
Out[15]: '/home/kristina/desire-directory'

使用Path.parent模塊中的pathlib

from pathlib import Path

# ...

Path(__file__).parent

您可以使用對parent的多次調用以在路徑中走得更遠:

Path(__file__).parent.parent

這對我有用(我在 Ubuntu 上):

import os
os.path.dirname(os.getcwd())
import os
current_file = os.path.abspath(os.path.dirname(__file__))
parent_of_parent_dir = os.path.join(current_file, '../../')

'..'返回當前目錄的父級。

import os
os.chdir('..')

現在您的當前目錄將是/home/kristina/desire-directory

你可以簡單地使用../your_script_name.py例如假設你的 python 腳本的路徑是trading system/trading strategies/ts1.py 參考位於trading system/data/volume.csv 您只需將其稱為../data/volume.csv

import os def parent_directory(): # 創建當前工作目錄的父級的相對路徑 path = os.getcwd() parent = os.path.dirname(path)

 relative_parent = os.path.join(path, parent) # Return the absolute path of the parent directory return relative_parent

打印(父目錄())

import os
import sys
from os.path import dirname, abspath

d = dirname(dirname(abspath(__file__)))
print(d)
path1 = os.path.dirname(os.path.realpath(sys.argv[0]))
print(path1)
path = os.path.split(os.path.realpath(__file__))[0]
print(path)

對我來說,這是有效的:

from os import path
path.dirname(path.dirname(__file__))

以當前文件為參考獲取當前目錄,然后再次調用path.dirname獲取父目錄。

from os.path import dirname
from os.path import abspath

def get_file_parent_dir_path():
    """return the path of the parent directory of current file's directory """
    current_dir_path = dirname(abspath(__file__))
    path_sep = os.path.sep
    components = current_dir_path.split(path_sep)
    return path_sep.join(components[:-1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM