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

How do I automatically convert code using os.path to use pathlib?

I have some code that uses os.path.join and all the other os.path methods. How can I convert it to use pathlib.Path objects everywhere. Is there a more efficient way than grepping for os.path and changing every instance?
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
4
votes
3 answers

Globbing Absolute Paths With Pathlib

In python3, when operating on unknown (user-input) file paths, I need to support wildcards such as ./r*/*.dat. The plan was to use something like this (simplified): paths = [] for test in userinputs: paths.extend(pathlib.Path().glob(test)) This…
Michael
  • 145
  • 1
  • 8
4
votes
1 answer

Is Path.replace equivalent to os.replace or shutil.move?

The documentation of the pathlib.Path.replace method states: Rename this file or directory to the given target. If target points to an existing file or directory, it will be unconditionally replaced. This lacks a bit of detail. For comparison,…
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
4
votes
1 answer

How do I perform a comparison using a generator object ( pathlib.iterdir ) in Python3?

I have a ton of files in a directory named calls. All of these files contain in their filenames their creation date, ex: 20181022_151012_kK029150d6.xml I need to find all the files whose creation date is >= 180 days old. I'm using pathlib to collect…
mr.zog
  • 533
  • 1
  • 7
  • 26
4
votes
1 answer

How do I use pathlib to create files named for range of dates?

I can print a range of filenames with the below code but I need to write empty files instead of printing. I know that pathlib.Path.touch and accomplish this but . . . do I need to define a method for the touch? import datetime import pathlib for…
mr.zog
  • 533
  • 1
  • 7
  • 26
4
votes
1 answer

How do I delete a directory tree with pathlib?

I have a project using pathlib and I want to do the equivalent of shutil.rmtree. I thought of doing it with something like: def pathlib_rmtree(folder): if folder.exists(): for file in folder.glob('**/*'): if file.is_dir(): …
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
4
votes
3 answers

Python: move a file up one directory

Below is a small test routine which takes a path to a file and moves the file up one directory. I am using the os and shutil modules, is there one module that could perform this task? Is there a more pythonic way of implementing this functionality?…
10SecTom
  • 2,484
  • 4
  • 22
  • 26
4
votes
2 answers

How to get sibling folder path in python?

x,y, and z are folders in d:/ d:/x(current) d:/y d:/z What is the best way to get paths of y and z folders from given x folder path.
letmecheck
  • 1,183
  • 8
  • 17
4
votes
1 answer

Can I use the undocumented method pathlib.Path.absolute()?

The first method found by auto-complete on a pathlib.Path is absolute(). It seems to just prepend Path.cwd() at the start: >>> from pathlib import Path >>> path = Path('./relative/path') /…
florisla
  • 12,668
  • 6
  • 40
  • 47
4
votes
2 answers

Why is Path(...).exists true even when the path doesn't exist?

I'm supposed to ask the user to enter a directory and if the directory doesn't exist, we tell them and then make a directory for them. This is my code so far. It acts in the same way whether or not the directory whose path is entered actually…
Accelerate
  • 65
  • 1
  • 6
4
votes
2 answers

pathlib.path allowing other types for joining apart from string and Path

How can I change pathlib.Path._parse_args, so that I cannot only have other types (like LocalPath) as arguments to Path but also use other types as parameter for / based joining, etc..? from pathlib import Path Path(tmplib) / 25 / True With tmplib…
Alois
  • 197
  • 2
  • 9
3
votes
2 answers

Match files starting with a number and underscore using glob

Initially I tried folder_path.glob('[0-9]_*.json') Where folder_path is a pathlib.Path object. But it only works for files that start with a single digit. after failing to find a proper match pattern, I used an additional condition to verify that…
Cristian
  • 405
  • 3
  • 10
3
votes
1 answer

Is Python 3 Path.write_text from pathlib atomic?

I was wondering if the Path.write_text(data) function from pathlib was atomic or not. If not, are there scenarios where we could end up with a file created in the filesystem but not containing the intended content? To be more specific, as the…
kriss
  • 23,497
  • 17
  • 97
  • 116
3
votes
3 answers

Get directories only with glob pattern using pathlib

I want to use pathlib.glob() to find directories with a specific name pattern (*data) in the current working dir. I don't want to explicitly check via .isdir() or something else. Input data This is the relevant listing with three folders as the…
buhtz
  • 10,774
  • 18
  • 76
  • 149
3
votes
1 answer

Doctest not working in Sphinx, cannot import python files Python

I have a problem trying to run doctest from the Sphinx Tutorial. I have the directory tree below but I cannot run a doctest for lumache.py. How would be able to make it so that lumache.py is importable with the pathlib function in conf.py which is…
Bosser445
  • 303
  • 1
  • 9