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
31
votes
7 answers

Clean way to get the "true" stem of a Path object?

Expected inputs and outputs: a -> a a.txt -> a archive.tar.gz -> archive directory/file -> file d.x.y.z/f.a.b.c -> f logs/date.log.txt -> date # Mine! Here's my implementation that feels dirty to me: >>> from…
Navith
  • 929
  • 1
  • 9
  • 15
27
votes
3 answers

How can I replace a substring in a Python pathlib.Path?

Is there an easy way to replace a substring within a pathlib.Path object in Python? The pathlib module is nicer in many ways than storing a path as a str and using os.path, glob.glob etc, which are built in to pathlib. But I often use files that…
Hector
  • 591
  • 2
  • 6
  • 13
26
votes
2 answers

Convert WindowsPath to String

redpath = os.path.realpath('.') thispath = os.path.realpath(redpath) fspec = glob.glob(redpath+'/*fits') thispath = os.path.realpath(thispath+'/../../../..') p = Path(thispath) userinput = 'n' while (userinput == 'n'): …
mhemmy
  • 297
  • 1
  • 4
  • 9
22
votes
1 answer

When should I use pathlib.Path.mkdir() vs os.mkdir() or os.makedirs()?

According to python 3.6 documentation, a directory can be created via: pathlib.Path.mkdir(mode=0o777, parents=False, exist_ok=False) os.mkdir(path, mode=0o777, *, dir_fd=None) os.makedirs(name, mode=0o777, exist_ok=False) Questions: It looks like…
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
20
votes
8 answers

How to glob two patterns with pathlib?

I want find two types of files with two different extensions: .jl and .jsonlines. I use from pathlib import Path p1 = Path("/path/to/dir").joinpath().glob("*.jl") p2 = Path("/path/to/dir").joinpath().glob("*.jsonlines") but I want p1 and p2 as one…
Gmosy Gnaq
  • 597
  • 1
  • 5
  • 18
19
votes
3 answers

Recommended way of closing files using pathlib module?

Historically I have always used the following for reading files in python: with open("file", "r") as f: for line in f: # do thing to line Is this still the recommend approach? Are there any drawbacks to using the following: from pathlib…
Ben Carley
  • 201
  • 1
  • 2
  • 7
19
votes
3 answers

What's the best way to add a trailing slash to a pathlib directory?

I have a directory I'd like to print out with a trailing slash: my_path = pathlib.Path('abc/def') Is there a nicer way of doing this than os.path.join(str(my_path), '')?
Tom
  • 42,844
  • 35
  • 95
  • 101
19
votes
2 answers

How to control order of result from iterator in python

I use pathlib.Path().iterdir() to get sub-dictionary of the path. Under /home/yuanyi/workspace/app, there are 4 folders: 01, 02, 03, 04. from pathlib import Path for subdir in Path('/home/yuanyi/workspace/app').iterdir(): print(subdir) But the…
wuyuanyi
  • 339
  • 1
  • 2
  • 8
18
votes
1 answer

How do I patch the `pathlib.Path.exists()` method?

I want to patch the exists() method of a pathlib.Path object for a unit test but I have problems getting this to work. What I am trying to do is this: from unittest.mock import patch from pathlib import Path def test_move_basic(): p =…
tfeldmann
  • 3,108
  • 1
  • 23
  • 34
16
votes
1 answer

pathlib.Path.relative_to vs os.path.relpath

I would like to find the relative path between two absolute paths. I have existing code that is generally using pathlib.Path for interacting with the filesystem, but I've run into a problem that seems easy to solve with os.path.relpath but (so far)…
larsks
  • 277,717
  • 41
  • 399
  • 399
16
votes
3 answers

How to test if object is a pathlib path?

I want to test if obj is a pathlib path and realized that the condition type(obj) is pathlib.PosixPath will be False for a path generated on a Windows machine. Thus the question, is there a way to test if an object is a pathlib path (any of the…
Matthias Arras
  • 565
  • 7
  • 25
16
votes
1 answer

Create symlink with pathlib

I want to test if Python code is working with symlinks properly. How can I create symlinks (e.g. equivalent to how os.symlink() can be used) in a faked filesystem based on pathlib.Path in a Python2/3 compatible way?
thinwybk
  • 4,193
  • 2
  • 40
  • 76
16
votes
3 answers

Why does the python pathlib Path('').exists() return True?

I was expecting Path('') to be a path that does not exist because it does not correspond to a file or directory name. Why is this considered to exist? from pathlib import Path print(Path('').exists()) I assume there is an advantage gained by…
mattm
  • 5,851
  • 11
  • 47
  • 77
16
votes
3 answers

How to take a pathname string with wildcards and resolve the glob with pathlib?

If I'm given a path as a string, such as "~/pythoncode/*.py" what is the best way to glob it in pathlib? Using pathlib, there is a way of appending to a path using a glob: p = pathlib.Path('~/pythoncode/').expanduser().glob('*.py') but this, for…
Omegaman
  • 2,189
  • 2
  • 18
  • 30
15
votes
2 answers

What is the difference between pathlib glob('*') and iterdir?

Suppose I'm writing code using pathlib and I want to iter over all the files in the same level of a directory. I can do this in two ways: p = pathlib.Path('/some/path') for f in p.iterdir(): print(f) p = pathlib.Path('/some/path') for f in…
kaki gadol
  • 1,116
  • 1
  • 14
  • 34
1 2
3
42 43