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?
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?
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.
basename[:-len(".tar.gz")] for this.git-1.7.8.tar. There is no way to correctly guess how many dots the caller wants to strip off, so splitext() only strips the last one. If you want to handle edge-cases like .tar.gz, you'll have to do it by hand. Obviously, you can't strip all the dots, since you'll end up with git-1.You can use stem method to get file name.
Here is an example:
from pathlib import Path
p = Path(r"\\some_directory\subdirectory\my_file.txt")
print(p.stem)
# my_file