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
190
votes
4 answers

Python pathlib make directories if they don’t exist

If I wanted to specify a path to save files to and make directories that don’t exist in that path, is it possible to do this using the pathlib library in one line of code?
Jasonca1
  • 4,848
  • 6
  • 25
  • 42
177
votes
4 answers

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…
EquipDev
  • 5,573
  • 10
  • 37
  • 63
162
votes
7 answers

How to identify whether a file is normal file or directory

How do you check whether a file is a normal file or a directory using python?
cash
159
votes
6 answers

Recursively iterate through all subdirectories using pathlib

How can I use pathlib to recursively iterate over all subdirectories of a given directory? p = Path('docs') for child in p.iterdir(): child only seems to iterate over the immediate children of a given directory. I know this is possible with…
Oblomov
  • 8,953
  • 22
  • 60
  • 106
152
votes
3 answers

How to get folder name, in which given file resides, from pathlib.path?

Is there something similar to os.path.dirname(path), but in pathlib?
trainset
  • 2,079
  • 3
  • 13
  • 17
151
votes
6 answers

Copy file with pathlib in Python

I try to copy a file with pathlib import pathlib import shutil my_file=pathlib.Path('/etc/hosts') to_file=pathlib.Path('/tmp/foo') shutil.copy(my_file, to_file) I get this exception: /home/foo_egs_d/bin/python…
guettli
  • 25,042
  • 81
  • 346
  • 663
149
votes
7 answers

PathLib recursively remove directory?

Is there any way to remove a directory and its contents in the PathLib module? With path.unlink() it only removes a file, with path.rmdir() the directory has to be empty. Is there no way to do it in one function call?
Jasonca1
  • 4,848
  • 6
  • 25
  • 42
143
votes
13 answers

Listing of all files in directory?

Can anybody help me create a function which will create a list of all files under a certain directory by using pathlib library? Here, I have a: I have c:\desktop\test\A\A.txt c:\desktop\test\B\B_1\B.txt c:\desktop\test\123.txt I expected to…
Akrios
  • 1,637
  • 2
  • 10
  • 12
124
votes
6 answers

Create new folder with pathlib and write files into it

I'm doing something like this: import pathlib p = pathlib.Path("temp/").mkdir(parents=True, exist_ok=True) with p.open("temp."+fn, "w", encoding ="utf-8") as f: f.write(result) Error message: AttributeError: 'NoneType' object has no…
Bondrak
  • 1,370
  • 2
  • 9
  • 15
119
votes
6 answers

Is there a Pathlib alternate for os.path.join?

I am currently accessing the parent directory of my file using Pathlib as follows: Path(__file__).parent When I print it, and this gives me the following output: print('Parent: ', Path(__file__).parent) #output /home/user/EC/main-folder The…
reinhardt
  • 1,873
  • 3
  • 9
  • 23
101
votes
7 answers

Adding another suffix to a path that already has a suffix with pathlib

I was converting some old Python code to use pathlib instead of os.path for most path-related operations, but I ended up with the following problem: I needed to add another extension to a path that already had an extension (not replace it). With…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
83
votes
3 answers

From stat().st_mtime to datetime?

What is the most idiomatic/efficient way to convert from a modification time retrieved from stat() call to a datetime object? I came up with the following (python3): from datetime import datetime, timedelta, timezone from pathlib import Path path =…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
82
votes
2 answers

Is there an idiomatic way to add an extension using Python's Pathlib?

I'm using Python's Pathlib and I want to take something like p = Path('/path/to/foo') And then try a couple of different extensions. I can do for ext in ['.txt', '.md', '.note'] filename = Path(str(p) + ext) but that feels a little awkward. Is…
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
81
votes
2 answers

How do I append a string to a Path in Python?

The following code: from pathlib import Path Desktop = Path('Desktop') SubDeskTop = Desktop + "/subdir" gets the following error: --------------------------------------------------------------------------- TypeError …
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
71
votes
3 answers

How can I change directory with Python pathlib

What is the intended way to change directory using the Python pathlib (Documentation) functionality? Lets assume I create a Path object as follows: from pathlib import Path path = Path('/etc') Currently I just know the following, but that seems to…
Lukas
  • 2,330
  • 2
  • 22
  • 31
1
2 3
42 43