从Python脚本获取当前目录的父级

时间:2021-12-31 20:36:12

I want to get the parent of current directory from Python script. For example I launch the script from /home/kristina/desire-directory/scripts the desire path in this case is /home/kristina/desire-directory

我想从Python脚本中获取当前目录的父级。例如,我从/ home / kristina / desire-directory / scripts启动脚本,在这种情况下,欲望路径是/ home / kristina / desire-directory

I know sys.path[0] from sys. But I don't want to parse sys.path[0] resulting string. Is there any another way to get parent of current directory in Python?

我从sys了解sys.path [0]。但我不想解析sys.path [0]结果字符串。有没有其他方法可以在Python中获取当前目录的父级?

5 个解决方案

#1


48  

Using os.path

To get the parent directory of the directory containing the script (regardless of the current working directory), you'll need to use __file__.

要获取包含脚本的目录的父目录(无论当前工作目录如何),您需要使用__file__。

Inside the script use os.path.abspath(__file__) to obtain the absolute path of the script, and call os.path.dirname twice:

在脚本内部使用os.path.abspath(__ file__)来获取脚本的绝对路径,并调用os.path.dirname两次:

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

Basically, you can walk up the directory tree by calling os.path.dirname as many times as needed. Example:

基本上,您可以根据需要多次调用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'

If you want to get the parent directory of the current working directory, use os.getcwd:

如果要获取当前工作目录的父目录,请使用os.getcwd:

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

Using pathlib

You could also use the pathlib module (available in Python 3.4 or newer).

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

Each pathlib.Path instance have the parent attribute referring to the parent directory, as well as the parents attribute, which is a list of ancestors of the path. Path.resolve may be used to obtain the absolute path. It also resolves all symlinks, but you may use Path.absolute instead if that isn't a desired behaviour.

每个pathlib.Path实例都具有引用父目录的parent属性,以及parent属性,该属性是路径的祖先列表。 Path.resolve可用于获取绝对路径。它还解析了所有符号链接,但如果不是所需的行为,则可以使用Path.absolute。

Path(__file__) and Path() represent the script path and the current working directory respectively, therefore in order to get the parent directory of the script directory (regardless of the current working directory) you would use

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')

and to get the parent directory of the current working directory

并获取当前工作目录的父目录

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

Note that d is a Path instance, which isn't always handy. You can convert it to str easily when you need it:

请注意,d是Path实例,并不总是很方便。您可以在需要时将其轻松转换为str:

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

#2


6  

This worked for me (I am on Ubuntu):

这对我有用(我在Ubuntu上):

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

#3


4  

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

#4


4  

You can use Path.parent from the pathlib module:

您可以使用pathlib模块中的Path.parent:

from pathlib import Path

# ...

Path(__file__).parent

You can use multiple calls to parent to go further in the path:

您可以使用多个父级调用在路径中进一步调用:

Path(__file__).parent.parent

#5


0  

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])

#1


48  

Using os.path

To get the parent directory of the directory containing the script (regardless of the current working directory), you'll need to use __file__.

要获取包含脚本的目录的父目录(无论当前工作目录如何),您需要使用__file__。

Inside the script use os.path.abspath(__file__) to obtain the absolute path of the script, and call os.path.dirname twice:

在脚本内部使用os.path.abspath(__ file__)来获取脚本的绝对路径,并调用os.path.dirname两次:

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

Basically, you can walk up the directory tree by calling os.path.dirname as many times as needed. Example:

基本上,您可以根据需要多次调用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'

If you want to get the parent directory of the current working directory, use os.getcwd:

如果要获取当前工作目录的父目录,请使用os.getcwd:

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

Using pathlib

You could also use the pathlib module (available in Python 3.4 or newer).

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

Each pathlib.Path instance have the parent attribute referring to the parent directory, as well as the parents attribute, which is a list of ancestors of the path. Path.resolve may be used to obtain the absolute path. It also resolves all symlinks, but you may use Path.absolute instead if that isn't a desired behaviour.

每个pathlib.Path实例都具有引用父目录的parent属性,以及parent属性,该属性是路径的祖先列表。 Path.resolve可用于获取绝对路径。它还解析了所有符号链接,但如果不是所需的行为,则可以使用Path.absolute。

Path(__file__) and Path() represent the script path and the current working directory respectively, therefore in order to get the parent directory of the script directory (regardless of the current working directory) you would use

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')

and to get the parent directory of the current working directory

并获取当前工作目录的父目录

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

Note that d is a Path instance, which isn't always handy. You can convert it to str easily when you need it:

请注意,d是Path实例,并不总是很方便。您可以在需要时将其轻松转换为str:

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

#2


6  

This worked for me (I am on Ubuntu):

这对我有用(我在Ubuntu上):

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

#3


4  

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

#4


4  

You can use Path.parent from the pathlib module:

您可以使用pathlib模块中的Path.parent:

from pathlib import Path

# ...

Path(__file__).parent

You can use multiple calls to parent to go further in the path:

您可以使用多个父级调用在路径中进一步调用:

Path(__file__).parent.parent

#5


0  

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])