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.
Katriel Aug 23 2018-10-23 14:56
source share