如何在Python中获得绝对文件路径?

时间:2022-09-06 13:26:08

Given a path such as "mydir/myfile.txt", how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:

给定一条路径,如“mydir/myfile”。在Python中,如何找到相对于当前工作目录的绝对文件路径?在Windows上,我可能会得到:

"C:/example/cwd/mydir/myfile.txt"

8 个解决方案

#1


671  

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'

Also works if it is already an absolute path:

如果它已经是一条绝对路径,也可以工作:

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'

#2


49  

>>> import os
>>> os.path.abspath('mydir/myfile.txt')
'C:\\example\\cwd\\mydir\\myfile.txt'
>>> 

#3


47  

You could use the new Python 3.4 library pathlib. (You can also get it for Python 2.6 or 2.7 using pip install pathlib.) The authors wrote: "The aim of this library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them."

您可以使用新的Python 3.4库pathlib。(您也可以使用pip安装pathlib来获得Python 2.6或2.7的版本)。作者写道:“这个库的目的是提供一个简单的类层次结构来处理文件系统路径,以及普通操作用户对它们的处理。”

To get an absolute path in Windows:

在Windows中获得绝对路径:

>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'

Or on UNIX:

或在UNIX:

>>> from pathlib import Path
>>> p = Path("python3.4").resolve()
>>> p
PosixPath('/opt/python3/bin/python3.4')
>>> str(p)
'/opt/python3/bin/python3.4'

Docs are here: https://docs.python.org/3/library/pathlib.html

文档在这里:https://docs.python.org/3/library/pathlib.html

#4


21  

Better still, install the path.py module, it wraps all the os.path functions and other related functions into methods on an object that can be used wherever strings are used:

最好还是安装路径。py模块,它封装了所有操作系统。路径函数和其他相关函数在一个对象上的方法,可以在任何字符串使用的地方使用:

>>> from path import path
>>> path('mydir/myfile.txt').abspath()
'C:\\example\\cwd\\mydir\\myfile.txt'
>>>

#5


10  

Today you can also use the unipath package which was based on path.py: http://sluggo.scrapping.cc/python/unipath/

今天,您还可以使用基于path的unipath包。py:http://sluggo.scrapping.cc/python/unipath/

>>> from unipath import Path
>>> absolute_path = Path('mydir/myfile.txt').absolute()
Path('C:\\example\\cwd\\mydir\\myfile.txt')
>>> str(absolute_path)
C:\\example\\cwd\\mydir\\myfile.txt
>>>

I would recommend using this package as it offers a clean interface to common os.path utilities.

我建议使用这个包,因为它提供了一个通用操作系统的干净界面。路径工具。

#6


3  

I prefer to use glob

我更喜欢用glob。

here is how to list all file types in your current folder:

下面是如何列出当前文件夹中的所有文件类型:

import glob
for x in glob.glob():
    print(x)

here is how to list all (for example) .txt files in your current folder:

下面是如何列出当前文件夹中的所有(例如).txt文件的方法:

import glob
for x in glob.glob('*.txt'):
    print(x)

here is how to list all file types in a chose directory:

下面是如何列出选择目录中的所有文件类型:

import glob
for x in glob.glob('C:/example/hi/hello/'):
    print(x)

hope this helped you

希望这有助于你

#7


0  

if you are on a mac

如果你在mac电脑上。

import os
upload_folder = os.path.abspath("static/img/users")

this will give you a full path:

这将给你一个完整的路径:

print(upload_folder)

will show the following path:

将显示以下路径:

>>>/Users/myUsername/PycharmProjects/OBS/static/img/user

#8


-1  

filePath = os.path.abspath(directoryName)
filePathWithSlash = filePath + "\\"
filenameWithPath = os.path.join(filePathWithSlash, filename)

#1


671  

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'

Also works if it is already an absolute path:

如果它已经是一条绝对路径,也可以工作:

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'

#2


49  

>>> import os
>>> os.path.abspath('mydir/myfile.txt')
'C:\\example\\cwd\\mydir\\myfile.txt'
>>> 

#3


47  

You could use the new Python 3.4 library pathlib. (You can also get it for Python 2.6 or 2.7 using pip install pathlib.) The authors wrote: "The aim of this library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them."

您可以使用新的Python 3.4库pathlib。(您也可以使用pip安装pathlib来获得Python 2.6或2.7的版本)。作者写道:“这个库的目的是提供一个简单的类层次结构来处理文件系统路径,以及普通操作用户对它们的处理。”

To get an absolute path in Windows:

在Windows中获得绝对路径:

>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'

Or on UNIX:

或在UNIX:

>>> from pathlib import Path
>>> p = Path("python3.4").resolve()
>>> p
PosixPath('/opt/python3/bin/python3.4')
>>> str(p)
'/opt/python3/bin/python3.4'

Docs are here: https://docs.python.org/3/library/pathlib.html

文档在这里:https://docs.python.org/3/library/pathlib.html

#4


21  

Better still, install the path.py module, it wraps all the os.path functions and other related functions into methods on an object that can be used wherever strings are used:

最好还是安装路径。py模块,它封装了所有操作系统。路径函数和其他相关函数在一个对象上的方法,可以在任何字符串使用的地方使用:

>>> from path import path
>>> path('mydir/myfile.txt').abspath()
'C:\\example\\cwd\\mydir\\myfile.txt'
>>>

#5


10  

Today you can also use the unipath package which was based on path.py: http://sluggo.scrapping.cc/python/unipath/

今天,您还可以使用基于path的unipath包。py:http://sluggo.scrapping.cc/python/unipath/

>>> from unipath import Path
>>> absolute_path = Path('mydir/myfile.txt').absolute()
Path('C:\\example\\cwd\\mydir\\myfile.txt')
>>> str(absolute_path)
C:\\example\\cwd\\mydir\\myfile.txt
>>>

I would recommend using this package as it offers a clean interface to common os.path utilities.

我建议使用这个包,因为它提供了一个通用操作系统的干净界面。路径工具。

#6


3  

I prefer to use glob

我更喜欢用glob。

here is how to list all file types in your current folder:

下面是如何列出当前文件夹中的所有文件类型:

import glob
for x in glob.glob():
    print(x)

here is how to list all (for example) .txt files in your current folder:

下面是如何列出当前文件夹中的所有(例如).txt文件的方法:

import glob
for x in glob.glob('*.txt'):
    print(x)

here is how to list all file types in a chose directory:

下面是如何列出选择目录中的所有文件类型:

import glob
for x in glob.glob('C:/example/hi/hello/'):
    print(x)

hope this helped you

希望这有助于你

#7


0  

if you are on a mac

如果你在mac电脑上。

import os
upload_folder = os.path.abspath("static/img/users")

this will give you a full path:

这将给你一个完整的路径:

print(upload_folder)

will show the following path:

将显示以下路径:

>>>/Users/myUsername/PycharmProjects/OBS/static/img/user

#8


-1  

filePath = os.path.abspath(directoryName)
filePathWithSlash = filePath + "\\"
filenameWithPath = os.path.join(filePathWithSlash, filename)