如何在Python中获得当前文件目录的完整路径?

时间:2022-07-22 16:02:23

I want to get the current file's directory path.
I tried:

我想要获取当前文件的目录路径。我试着:

>>> os.path.abspath(__file__)
'C:\\python27\\test.py'

But how can I retrieve the directory's path? For example:

但是如何检索目录的路径呢?例如:

'C:\\python27\\'

6 个解决方案

#1


926  

If you mean the directory of the script being run:

如果您指的是正在运行的脚本的目录:

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

If you mean the current working directory:

如果你指的是当前工作目录:

import os
os.getcwd()

Note that before and after file is two underscores, not just one.

注意,在文件之前和之后是两个下划线,而不是一个。

#2


7  

import os
print os.path.dirname(__file__)

#3


5  

You can use os and os.path library easily as follows

你可以使用操作系统和操作系统。路径库很简单,如下所示。

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

os.path.dirname returns upper directory from current one. It lets us change to an upper level without passing any file argument and without knowing absolute path.

os.path。dirname从当前目录返回上级目录。它允许我们在不传递任何文件参数的情况下更改到更高的级别,而不需要知道绝对路径。

#4


1  

In Python 3:

在Python 3:

from pathlib import Path

myPath = Path().absolute()
print("{}".format(myPath))

#5


0  

To keep the migration consistency across platforms (macOS/Windows/Linux), try:

为了保持跨平台的迁移一致性(macOS/Windows/Linux),请尝试:

path = r'%s' % os.getcwd().replace('\\','/')

#6


-1  

IPython has a magic command %pwd to get the present working directory. It can be used in following way:

IPython有一个神奇的命令%pwd来获取当前工作目录。可采用以下方式:

from IPython.terminal.embed import InteractiveShellEmbed

ip_shell = InteractiveShellEmbed()

present_working_directory = ip_shell.magic("%pwd")

On IPython Jupyter Notebook %pwd can be used directly as following:

在IPython Jupyter笔记本%pwd可以直接使用如下:

present_working_directory = %pwd

#1


926  

If you mean the directory of the script being run:

如果您指的是正在运行的脚本的目录:

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

If you mean the current working directory:

如果你指的是当前工作目录:

import os
os.getcwd()

Note that before and after file is two underscores, not just one.

注意,在文件之前和之后是两个下划线,而不是一个。

#2


7  

import os
print os.path.dirname(__file__)

#3


5  

You can use os and os.path library easily as follows

你可以使用操作系统和操作系统。路径库很简单,如下所示。

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

os.path.dirname returns upper directory from current one. It lets us change to an upper level without passing any file argument and without knowing absolute path.

os.path。dirname从当前目录返回上级目录。它允许我们在不传递任何文件参数的情况下更改到更高的级别,而不需要知道绝对路径。

#4


1  

In Python 3:

在Python 3:

from pathlib import Path

myPath = Path().absolute()
print("{}".format(myPath))

#5


0  

To keep the migration consistency across platforms (macOS/Windows/Linux), try:

为了保持跨平台的迁移一致性(macOS/Windows/Linux),请尝试:

path = r'%s' % os.getcwd().replace('\\','/')

#6


-1  

IPython has a magic command %pwd to get the present working directory. It can be used in following way:

IPython有一个神奇的命令%pwd来获取当前工作目录。可采用以下方式:

from IPython.terminal.embed import InteractiveShellEmbed

ip_shell = InteractiveShellEmbed()

present_working_directory = ip_shell.magic("%pwd")

On IPython Jupyter Notebook %pwd can be used directly as following:

在IPython Jupyter笔记本%pwd可以直接使用如下:

present_working_directory = %pwd