Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 follow a pattern, and often replace substrings in a path to access other files:

data/demo_img.png
data/demo_img_processed.png
data/demo_spreadsheet.csv

Previously I could do:

img_file_path = "data/demo_img.png"
proc_img_file_path = img_file_path.replace("_img.png", "_img_proc.png")
data_file_path = img_file_path.replace("_img.png", "_spreadsheet.csv")

pathlib can replace the file extension with the with_suffix() method, but only accepts extensions as valid suffixes. The workarounds are:

import pathlib
import os


img_file_path = pathlib.Path("data/demo_img.png")
proc_img_file_path = pathlib.Path(str(img_file_path).replace("_img.png", "_img_proc.png"))
# os.fspath() is available in Python 3.6+ and is apparently safer than str()
data_file_path = pathlib.Path(os.fspath(img_file_path).replace("_img.png", "_img_proc.png"))

Converting to a string to do the replacement and reconverting to a Path object seems laborious. Assume that I never have a copy of the string form of img_file_path, and have to convert the type as needed.

like image 715
Hector Avatar asked Nov 20 '18 18:11

Hector


People also ask

What does Pathlib path () do?

The pathlib is a Python module which provides an object API for working with files and directories. The pathlib is a standard module. Path is the core object to work with files.

How do I change a path in Python?

replace() method in Python is used to rename the file or directory. If destination is a directory, OSError will be raised. If the destination exists and is a file, it will be replaced without error if the action performing user has permission.

What is path () in Python?

PYTHONPATH is an environment variable which the user can set to add additional directories that the user wants Python to add to the sys. path directory list. In short, we can say that it is an environment variable that you set before running the Python interpreter.

Why You Should Use Pathlib?

Pathlib allows you to easily iterate over that directory's content and also get files and folders that match a specific pattern. Remember the glob module that you used to import along with the os module to get paths that match a pattern?


1 Answers

You are correct. To replace old with new in Path p, you need:

p = Path(str(p).replace(old, new))

EDIT

We turn Path p into str so we get this str method:

Help on method_descriptor:

replace(self, old, new, count=-1, /)

Return a copy with all occurrences of substring old replaced by new.

Otherwise we'd get this Path method:

Help on function replace in module pathlib:

replace(self, target)

Rename this path to the given path, clobbering the existing destination if it exists, and return a new Path instance pointing to the given path.

like image 144
J_H Avatar answered Sep 17 '22 15:09

J_H