Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 there a better way to do this?

like image 295
Wayne Werner Avatar asked Jul 16 '18 16:07

Wayne Werner


People also ask

What is the use of Pathlib in Python?

Fortunately, if you're coding in Python, the Pathlib module does the heavy lifting by letting you make sure that your file paths work the same in different operating systems. Also, it provides functionalities and operations to help you save time while handling and manipulating paths.

What is Purepath in Python?

Pure path objects provide path-handling operations which don't actually access a filesystem. Concrete paths are subclasses of the pure path classes. In addition to operations provided by the former(pure path), they also provide methods to do system calls on path objects.

Is Pathlib part of Python?

The pathlib module was introduced in Python 3.4 (PEP 428) to deal with these challenges. It gathers the necessary functionality in one place and makes it available through methods and properties on an easy-to-use Path object.

Is file a Pathlib?

The pathlib module allows you to manipulate files and folders using the object-oriented approach. If you're not familiar with object-oriented programming, check out the Python OOP section. If the file doesn't exist, the is_file() method returns False . Otherwise, it returns True .


2 Answers

The with_suffix method will return a new path with a different extension, either changing an existing extension or adding a new one. Examples from the docs:

>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') >>> p.with_suffix('.bz2') PureWindowsPath('c:/Downloads/pathlib.tar.bz2') >>> p = PureWindowsPath('README') >>> p.with_suffix('.txt') PureWindowsPath('README.txt') 

In your case, p.with_suffix(ext) would do the job.

For cases where you need to add a suffix after any existing suffixes instead of removing existing suffixes, you can use p.with_suffix(p.suffix+ext). This is kind of clunky, though, and I don't know whether I would prefer it over Path(str(p)+ext).

like image 57
user2357112 supports Monica Avatar answered Sep 22 '22 19:09

user2357112 supports Monica


You might use pathlib3x - it offers a backport of the latest (at the date of writing this answer Python 3.10.a0) Python pathlib for Python 3.6 or newer, and a few additional functions like append_suffix

>>> python -m pip install pathlib3x  >>> import pathlib3x as pathlib  >>> pathlib.Path('some_path').append_suffix('.ext') PosixPath('some_path.ext')  >>> pathlib.Path('some_path.ext.ext2').append_suffix('.ext3') PosixPath('some_path.ext.ext2.ext3')  >>> pathlib.Path('some_path.ext').append_suffix('.tar.gz') PosixPath('some_path.ext.tar.gz')   

you can find it on github or PyPi


Disclaimer: I'm the author of the pathlib3x library.

like image 28
bitranox Avatar answered Sep 22 '22 19:09

bitranox