138

I was converting some old Python code to use pathlib instead of os.path for most path-related operations, but I ended up with the following problem: I needed to add another extension to a path that already had an extension (not replace it). With os.path, since we are merely manipulating strings, the solution was to add the extension with string operations:

newpath = path + '.res' 

It doesn't work with pathlib.Path because it doesn't allow concatenation of arbitrary characters. The closest I could find was the following:

newpath = path.with_suffix(path.suffix + '.res') 

It looks like a workaround because it still uses string addition in the end. And it has a new pitfall because I forgot at first to handle the case where there are already several extensions and you want to add a new one, leading to the following code to get back the old behaviour:

newpath = path.with_suffix(''.join(path.suffixes) + '.res') 

Now it doesn't feel terse nor clean since it uses more and more string operations to achieve the old behaviour instead of pure path operations. The fact that Path.suffixes exists means that the library's developers considered the case where a file can have multiple extensions, yet I couldn't find a way to simply add a new extension to a path. Is there a more idiomatic way that I have missed to achieve the same behaviour?

EDIT: actually path.with_suffix(path.suffix + '.res') is enough to handle the case where there are already several file extensions, even though it wasn't immeditely obvious to me.

3
  • 5
    it seems there was no better answer than the one-liner you already had. pathlib seems to have some very annoying fatal flaws that make it more difficult to work with than plain strings -- case in point, if you wanted to add the suffix _foo.res with your method, you get ValueError: Invalid suffix, because it doesn't start with a dot! Commented Jun 27, 2019 at 14:09
  • @spinup, I don't claim there are no flaws. But this particular behaviour seem quite correct imho: a suffix in term of pathnames are seperated by a .. You should do it with name, i.e. path.with_name(path.name + "_foo.res"). Commented Jan 28, 2022 at 17:59
  • @doak Looking at this years after my original comment, I think I was looking at the case of changing a pathname from /a/b/c/file.ext to /a/b/c/file_foo.ext. The with_suffix method would seem to fit the bill perfectly to strip off the extension and add a new ending to the name, but for some reason they chose to artificially limit the method and throw an error instead. Commented Jan 28, 2022 at 21:56

8 Answers 8

127

I find the following slightly more satisfying than the answers that have already been given:

new_path = path.parent / (path.name + '.suffix') 

EDIT: as @mitch-mcmabers pointed out, an even more satisfying answer would be

new_path = path.with_name(f"{path.name}.suffix") 
Sign up to request clarification or add additional context in comments.

4 Comments

Ever so slightly more streamlined version: new_path = path.parent / f"{path.name}.suffix"
Or simply inject a full string cast: Path(f"{path}.suffix")
Is this answer correct? Pathlib docs specify .name to be with suffix
@Xerusial I don't see an example of what OP exactly wants, but I think he wants a filename with extension and add an extra extension (for example filte.txt to file.txt.backup), otherwise the .with_suffix function would suffice.
38

It doesn't seem like Path's like being modified in-place (you can't change .parts[-1] directory or change .suffixes, etc.), but that doesn't mean you need to resort to anything too unsavory. The following works just fine, even if it's not quite as elegant as I'd like:

new_path = path.with_suffix(path.suffix + new_suffix) 

where path is your original Path variable, and new_suffix is the string with your new suffix/extension (including the leading ".")

4 Comments

If you are going to write out both extensions, it would probably be more canonical to write as new_path = path.with_suffix(new_suffix)
your answer would substitute the suffix, not add it
@ImanolUr I assumed "new_suffix" would include everything desired. See the edited code for a more intuitive answer.
But... path.parent / path.name is exactly equivalent to the original path so how is that different from OP's path.with_suffix(path.suffix + '.res')?
24

I think this would be better since you just want to extend the current path with an arbitrary string.

old_path = Path("/the/old/path.foo") # "/the/old/path.foo" new_path = Path(f"{old_path}.bar") # "/the/old/path.foo.bar" 

3 Comments

This is surprisingly the most readable code. Pathlib went too far with suffixes and forgot the most simple thing.
@march That can't be said enough. In all of the praise for pathlib, it seems like folks forgot the principle that simple things should be simple... especially things that used to be simple and are now hard, like appending a suffix.
I like it. Its simple and clear, and easy-readable
15

You might use pathlib3x - it offers a backport of the latest (at the date of writing this answer Python 3.11.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') 

you can find it on github or PyPi


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

5 Comments

Do you know how maintained that repo will be in the future?
Cool, now that's something I would like in the standard library :o
@Dash: it will be maintained as long as I live - then You can take over ;-) since I use it every day ...
@Morwenn that was one of the reasons I did it - on one hand it is clear that devs dont want to clutter the standard library, on the other hand I want to add useful functions, in a way that is compatible and in line with the style of the standard library. Just to make my own code more readable and less verbose.
That's really convenient, thanks! However when passing such a path object to joblib I got a ValueError: Second argument should be a filename or a file-like object, ../data/processed/blabla.pkl (type <class 'pathlib3x.pathlib3x.PosixPath'>) was given., which does't happen with the standard pathlib objects. Is it because joblib doesn't recognizes pathlib3x.pathlib3x.PosixPath as a PosixPath? In the meantime I just need to add .as_posix(), but it would be great if we wouldn't have to.
14

You can just convert your Path to string then add new extension and convert back to Path:

from pathlib import Path first = Path("D:/user/file.xy") print(first) second = Path(str(first)+".res") print(second) 

1 Comment

You can also monkey patch it: Path.__add__ = lambda self, rhs: Path(str(self) + rhs)
1

if you want to append the file name, but not change the extension, this works

matfile2 = pathlib.Path.joinpath(matfile.parent, matfile.stem+' insert'+matfile.suffix) 

Comments

0

An f-string is probably most concise:

>>> import pathlib >>> p = pathlib.Path( '/some/where/file/name.log' ) >>> p.with_suffix( f'{p.suffix}.bz2' ) PosixPath( '/some/where/file/name.log.bz2' ) 

Comments

-2

The following code should do what you want it to in a very dynamic way.

from pathlib import Path import time p = Path('.') p = p / '..' / 'Python' / 'Files' / 'Texts_to_read' / 'a_text_file' new_p = str(p).split('\\') new_suffix = '.txt' new_p[-1] = new_p[-1] + new_suffix p = Path('.') for x in new_p: p = p / x print(new_p) print(p) print(str(p)) time.sleep(5) 

The fact that normal string operations can be used in this case is a good thing, as it adds a great deal of control over the file path desired without requiring a large assortment of new functions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.