Questions tagged [pathlib]

This tag is for questions about the Python built-in library pathlib, which offers classes representing filesystem paths with semantics appropriate for different operating systems.

This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between Pure paths, which provide purely computational operations without I/O, and Concrete paths, which inherit from pure paths but also provide I/O operations.

Its strength lies in that it offers a powerful Object Oriented interface for working with paths, as opposed to the string-based interface offered by os.path.

This table offers a mapping between various os functions to their corresponding PurePath/Path equivalent.

635 questions
58
votes
2 answers

Renaming file extension using pathlib (python 3)

I am using windows 10 and winpython. I have a file with a .dwt extension (it is a text file). I want to change the extension of this file to .txt. My code does not throw any errors, but it does not change the extension. from pathlib import…
user3398600
  • 685
  • 1
  • 5
  • 8
58
votes
1 answer

Python Pathlib path object not converting to string

I am trying to use Shutil to copy a pdf file using path objects from Pathlib, however when I run my code I get the error "str object is not callable" when converting my paths back to strings using str(). Any explanation for why this is occurring…
MrClean
  • 1,300
  • 2
  • 12
  • 17
57
votes
1 answer

When using pathlib, getting error: TypeError: invalid file: PosixPath('example.txt')

I'm using Python 3's pathlib module, like this: from pathlib import Path filename = Path(__file__).parent / "example.txt" contents = open(filename, "r").read() But I get this error on some machines: TypeError: invalid file:…
Flimm
  • 136,138
  • 45
  • 251
  • 267
53
votes
1 answer

Pathlib vs. os.path.join in Python

When I need to define a file system path in my script, I use os.path.join to guarantee that the path will be consistent on different file systems: from os import path path_1 = path.join("home", "test", "test.txt") I also know that there is Pathlib…
Amir
  • 1,926
  • 3
  • 23
  • 40
52
votes
2 answers

python pathlib operator '/' - how does it do it?

I found the pathlib syntax - or is it the Python syntax - surprising. I'd like to know how this makes the forward slash / act as a joiner of WindowsPaths etc. Does it override/overload / ? It seems to be in a magical context, the slash is between a…
JoePythonKing
  • 1,080
  • 1
  • 9
  • 18
50
votes
6 answers

Return total number of files in directory and subdirectories

Trying to create a function that returns the # of files found a directory and its subdirectories. Just need help getting started
Bob
  • 539
  • 2
  • 5
  • 11
47
votes
7 answers

pathlib Path `write_text` in append mode

Is there a shortcut for python pathlib.Path objects to write_text() in append mode? The standard open() function has mode="a" to open a file for writing and appending to the file if that file exists, and a Paths .open() function seems to have the…
danodonovan
  • 19,636
  • 10
  • 70
  • 78
44
votes
2 answers

Python's pathlib get parent's relative path

Consider the following Path: import pathlib path = pathlib.Path(r'C:\users\user1\documents\importantdocuments') How can I extract the exact string documents\importantdocuments from that Path? I know this example looks silly, the real context here…
Mojimi
  • 2,561
  • 9
  • 52
  • 116
44
votes
1 answer

Loop over results from Path.glob() (Pathlib)

I'm struggling with the result of the Path.glob() method of the Pathlib module in Python 3.6. from pathlib import Path dir = Path.cwd() files = dir.glob('*.txt') print(list(files)) >> [WindowsPath('C:/whatever/file1.txt'),…
keyx
  • 567
  • 1
  • 4
  • 5
43
votes
3 answers

Using pathlib's relative_to for directories on the same level

The python library pathlib provides Path.relative_to. This function works fine if one path is a subpath of the other one, like this: from pathlib import Path foo = Path("C:\\foo") bar = Path("C:\\foo\\bar") bar.relative_to(foo) >…
JFB
  • 935
  • 2
  • 8
  • 12
35
votes
2 answers

How to make a new Path object from parts of a current path with pathlib?

I would like to change a part of a Path object with pathlib. For example if you have a Path object: import pathlib path = pathlib.Path("/home/user/to/some/floder/toto.out") How can I change the file name ? And get a new path with for example…
Ger
  • 9,076
  • 10
  • 37
  • 48
35
votes
8 answers

Use pathlib for S3 paths

I would like to build some functionality to move files between S3 and my local file system, but pathlib appears to combine repeated slashes, breaking my aws-cli functionality: >>> from pathlib import Path >>> str(Path('s3://loc')) s3:/loc' How can…
beardc
  • 20,283
  • 17
  • 76
  • 94
34
votes
1 answer

Why does pathlib have both PurePath & Path?

More than an answer to the question, I am trying to learn how to make sense of the Official Python Documentation. I understand that Path inherits from PurePath, but I am unable to understand when to use which and why there is PurePath & Path instead…
user7579349
  • 581
  • 1
  • 5
  • 10
33
votes
4 answers

Adding a directory to sys.path with pathlib

I'm trying to add a directory to PATH with code like this: PROJECT_DIR = Path(__file__).parents[2] sys.path.append( PROJECT_DIR / 'apps' ) It doesn't work. If I do print sys.path I see something like this: [...,…
kharandziuk
  • 12,020
  • 17
  • 63
  • 121
32
votes
1 answer

What is the correct way in python to annotate a path with type hints?

What is the proper way to annotate this simple utility function in python3 that reads from a file? It should accept pathlib.Path objects as well as any other common way of passing a path. def read_json(path: ): with open(path, 'rb')…
Zacharias030
  • 509
  • 1
  • 5
  • 10
1
2
3
42 43