How to replace (or remove) an extension from a file name in Python?

Is there a built-in function in Python that will replace (or remove, whatever) the file name extension (if any)?

Example:

print replace_extension('/home/user/somefile.txt', '.jpg') 

In my example: /home/user/somefile.txt will become /home/user/somefile.jpg

I do not know if this matters, but I need this for the SCons module that I am writing. (Maybe there is some special SCons function that I can use?)

I need to clean something. Doing a simple line replacement of all .txt occurrences within a line is obviously not clean. (This will fail if my file name is somefile.txt.txt.txt )

+75
python scons
Aug 23 2018-10-23T00:
source share
7 answers

Try os.path.splitext , it should do what you want.

 import os print os.path.splitext('/home/user/somefile.txt')[0]+'.jpg' 
+107
Aug 23 '10 at 14:51
source share

Deploying AnaPana's answer, how to remove extension using pathlib (Python> = 3.4):

 >>> from pathlib import Path >>> filename = Path('/some/path/somefile.txt') >>> filename_wo_ext = filename.with_suffix('') >>> filename_replace_ext = filename.with_suffix('.jpg') >>> print(filename) /some/path/somefile.ext >>> print(filename_wo_ext) /some/path/somefile >>> print(filename_replace_ext) /some/path/somefile.jpg 
+42
Feb 17 '17 at 1:58
source share

As @jethro said, splitext is a neat way to do this. But in this case, it is quite easy to split it yourself, since the extension should be part of the file name following the last period:

 filename = '/home/user/somefile.txt' print( filename.rsplit( ".", 1 )[ 0 ] ) # '/home/user/somefile' 

rsplit tells Python to perform string separations starting on the right side of the string, and 1 says to do no more than one split (so, for example, 'foo.bar.baz' β†’ [ 'foo.bar', 'baz' ] ). Since rsplit always returns a non-empty array, we can safely index 0 into it to get the file name minus the extension.

+27
Aug 23 2018-10-23
source share

I prefer the following single-line approach using str.rsplit () :

 my_filename.rsplit('.', 1)[0] + '.jpg' 

Example:

 >>> my_filename = '/home/user/somefile.txt' >>> my_filename.rsplit('.', 1) >>> ['/home/user/somefile', 'txt'] 
+4
Nov 18 '16 at 6:55
source share

Another way is to use the str.rpartition(sep) method.

For example:

 filename = '/home/user/somefile.txt' (prefix, sep, suffix) = filename.rpartition('.') new_filename = prefix + '.jpg' print new_filename 
+3
Dec 17 '13 at 19:33
source share

For Python> = 3.4:

 from pathlib import Path filename = '/home/user/somefile.txt' p = Path(filename) new_filename = p.parent.joinpath(p.stem + '.jpg') # PosixPath('/home/user/somefile.jpg') new_filename_str = str(new_filename) # '/home/user/somefile.jpg' 
+3
Feb 10 '17 at 0:36
source share

Handling Multiple Extensions

In case you have multiple extensions, this one-liner using pathlib and str.replace works:

Remove / Remove Extensions

 >>> from pathlib import Path >>> p = Path("/path/to/myfile.tar.gz") >>> str(p).replace("".join(p.suffixes), "") '/path/to/myfile' 

Replace Extensions

 >>> p = Path("/path/to/myfile.tar.gz") >>> new_ext = ".jpg" >>> str(p).replace("".join(p.suffixes), new_ext) '/path/to/myfile.jpg' 

If you also need the pathlib object then you can explicitly wrap the string in Path()

 >>> Path(str(p).replace("".join(p.suffixes), "")) PosixPath('/path/to/myfile') 
0
Jun 28 '19 at 13:33
source share



All Articles