Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

325
Visualizações
How to get absolute path of a pathlib.Path object?

Making a path object with pathlib module like:

p = pathlib.Path('file.txt')

The p object will point to some file in the filesystem, since I can do for example p.read_text().

How can I get the absolute path of the p object in a string?

Appears that I can use for example os.path.abspath(p) to get the absolute path, but it awkward to use an os.path method, since I assume that pathlib should be a replacement for os.path.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Use resolve()

Simply use Path.resolve() like this:

p = p.resolve()

This makes your path absolute and replaces all relative parts with absolute parts, and all symbolic links with physical paths. On case-insensitive file systems, it will also canonicalize the case (file.TXT becomes file.txt).

Avoid absolute() before Python 3.11

The alternative method absolute() was not documented or tested before Python 3.11 (See the discussion in the bug report created by @Jim Fasarakis Hilliard).

Fixes were merged in January 2022.

The difference

The difference between resolve and absolute is that absolute() does not replace the symbolically linked (symlink) parts of the path, and it never raises FileNotFoundError. It does not modify the case either.

If you want to avoid resolve() (e.g. you want to retain symlinks, casing, or relative parts) then use this instead on Python <3.11:

p = Path.cwd() / "file.txt"

This works even if the path you are supplying is absolute -- in that case the cwd (current working directory) is ignored.

Beware non-existing file on Windows

If the file does not exist, in Python 3.6 to 3.9 on Windows, resolve() does not prepend the current working directory. See issue 38671, fixed in Python 3.10.

Beware FileNotFoundError

On Python versions predating v3.6, resolve() does raise a FileNotFoundError if the path is not present on disk.

So if there's any risk to that, either check beforehand with p.exists() or try/catch the error.

# check beforehand
if p.exists():
    p = p.resolve()

# or except afterward
try:
    p = p.resolve()
except FileNotFoundError:
    # deal with the missing file here
    pass

If you're dealing with a path that's not on disk, to begin with, and you're not on Python 3.6+, it's best to revert to os.path.abspath(str(p)).

From 3.6 on, resolve() only raises FileNotFoundError if you use the strict argument.

# might raise FileNotFoundError
p = p.resolve(strict=True)

But beware, using strict makes your code incompatible with Python versions predating 3.6 since those don't accept the strict argument.

over 4 years ago · Santiago Trujillo Relatório

0

You're looking for the method .absolute, if my understanding is correct, whose documentation states:

>>> print(p.absolute.__doc__)
Return an absolute version of this path.  This function works
        even if the path doesn't point to anything.

        No normalization is done, i.e. all '.' and '..' will be kept along.
        Use resolve() to get the canonical path to a file.

With a test file on my system this returns:

>>> p = pathlib.Path('testfile')
>>> p.absolute()
PosixPath('/home/jim/testfile')

This method seems to be a new, and still, undocumented addition to Path and Path inheritting objects.

Created an issue to document this.

over 4 years ago · Santiago Trujillo Relatório

0

If you simply want the path and do not want to check if the file exists, you can do

str(p)

as document in the Operations section.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda