If I have a filename like one of these:
1.1.1.1.1.jpg 1.1.jpg 1.jpg
How could I get only the filename, without the extension? Would a regex be appropriate?
The standard solution is to use the os. path. splitext(path) function to split a path into a (root, ext) pair such that root + ext == path. This returns the path to the file without extension.
GetFileNameWithoutExtension(ReadOnlySpan<Char>) Returns the file name without the extension of a file path that is represented by a read-only character span.
In most cases, you shouldn't use a regex for that.
os.path.splitext(filename)[0]
This will also handle a filename like .bashrc
correctly by keeping the whole name.
>>> import os >>> os.path.splitext("1.1.1.1.1.jpg") ('1.1.1.1.1', '.jpg')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With