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
3
votes
1 answer

Difference between "joinpath" and "/" in Pathlib

Is there a difference between joinpath and the / operator in the pathlib module? The documentation doesn't ever compare the two methods. Essentially are there any cases where these two are different? Example: from pathlib import Path foo =…
3
votes
1 answer

Is there a pathlib equivalent to os.path.basename under Windows?

When running Python 3.9.5 on Windows 10: Path("c:/temp/one/two/").name == "two" PurePath("c:/temp/one/two/").name == "two" PureWindowsPath("c:/temp/one/two/").name == "two" while os.path.basename("c:/temp/one/two/").name == ""
adom
  • 41
  • 3
3
votes
1 answer

How to check whether a path is in another path using Pathlib?

I'm trying to find a readable way to check whether a path is within another path across more than a single level. I can check the immediate parent, but not beyond that so far. import pathlib path1 = pathlib.Path('/a/b') path2 =…
tarabyte
  • 17,837
  • 15
  • 76
  • 117
3
votes
3 answers

Make pathlib.glob() and pathlib.rglob() case-insensitive for platform-agnostic application

I am using pathlib.glob() and pathlib.rglob() to matching files from a directory and its subdirectories, respectively. Target files both are both lower case .txt and upper case .TXT files. According file paths were read from the filesystem as…
albert
  • 8,027
  • 10
  • 48
  • 84
3
votes
1 answer

How to list directory files excluding files in gitignore?

I am trying to list all directory files, but without files (or patterns) from .gitignore. For example, script runs inside main directory: # .gitignore dir2/ py.cache # directory structure . ├── .gitignore ├── subdir │ ├── dir1 │ │   ├── file1 │…
Max Smirnov
  • 477
  • 3
  • 10
3
votes
1 answer

Python - explanation of pathlib.Path.mkdir mode argument

Please help understand how mode=511 is translated into the directory permission "775" in Pathlib.Path.mkdir(). Path.mkdir(mode=511, parents=False, exist_ok=False) Path.mkdir(mode=511, parents=False, exist_ok=False) Create a new directory at this…
mon
  • 18,789
  • 22
  • 112
  • 205
3
votes
2 answers

Recursive globbing of fast-changing directories

I stumbled upon unintuitive problem: if you recursively glob (with pathlib) fast-changing directory, where other directories are created and deleted, you have a chance to get FileNotFoundException. If directory is not found, then why bother me with…
3
votes
1 answer

python: transforming os.Path code into Pathlib code

I have the following function in python to add a dict as row to a pandas DF that also takes care of creating a first empty DF if there is not yet there. I use the library os but I would like to change to Pathlib since consulting with a Software…
JFerro
  • 3,203
  • 7
  • 35
  • 88
3
votes
1 answer

pathlib: cannot import name 'Sequence' from 'collections'

It has been a few days since I rebuilt my project but when I was testing some things this morning I wanted to update my Werkzeug package due to an issue I was having with its Multidict class, I rebuilt and started getting this error: #17 73.14 …
Lucas Rahn
  • 185
  • 1
  • 4
  • 10
3
votes
2 answers

How can you prefix a Python pathlib.Path with another path?

I have a pathlib.Path('/etc'). If I need to prefix it with pathlib.Path('/mnt/chroot') and do something like: Path('/mnt/chroot') / Path('/etc') I just end up with: PosixPath('/etc'), presumably because both Path's are absolute paths, and can't be…
John
  • 2,551
  • 3
  • 30
  • 55
3
votes
1 answer

Python pathlib.unlink: 'str' object has no attribute '_accessor'

I am trying to use Path.unlink to remove test files in a pytest fixture, but I keep getting this error on teardown. The fixture teardown code: yield try: for i in range(2): file = db_helper(f'testfile{i}') …
Chris
  • 126
  • 1
  • 2
  • 9
3
votes
1 answer

Pathlib and stem - Attributerror

as part of a code I have function as follow: def match_output(orig_path: Path,lines: Iterable[str],stem: str, delim: str,delim_pred: Callable[[int], bool],) -> Iterable: n = 0 path = orig_path.with_stem(f'{orig_path.stem}_{stem}') with…
Sam.H
  • 193
  • 2
  • 14
3
votes
1 answer

InvalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]

I have a folder structure where every subfolder represents a class, for every class there is exactly one example picture. I want to load the data in a Keras dataset as described here:…
Martin1997
  • 146
  • 2
  • 14
3
votes
2 answers

Airflow issue with pathlib / configparser - 'PosixPath' object is not iterable

I am trying to containerize my airflow setup. I've been tasked to keep the environment the same, just move it into a docker container. We currently have Airflow and all our dependencies installed within a anaconda environment. So what I've done is…
wymangr
  • 189
  • 3
  • 16
3
votes
4 answers

How do you combine more than one pathlib object?

I've got two Path objects using Python's pathlib library, pathA = Path('/source/parent') and pathB = Path('/child/grandchild'). What's the most direct way to combine the two so that you get a Path('/source/parent/child/grandchild') object?
Sanjan Das
  • 45
  • 1
  • 8