I have a problem with deleting extension from my filename. I tried to use
, but it delete only
.gz from filename. I need to check if file has extension or it’s a directory. I did it using this:
But another problem is, that I can’t split it cause of data in it (YYYY.MM.DD). Should I use
join() or it is something more attractive instead of tons of methods and ifs?
If you know that the extension is always going to be .tar.gz, you can still use split:
In : fname = 'RANDOM_FILE-2017.06.07.tar.gz' In : '.'.join(fname.split('.')[:-2]) Out: 'RANDOM_FILE-2017.06.07'
From the docstring for os.path.splitext:
"Extension is everything from the last dot to the end, ignoring leading dots. "
In the case of gzipped tarballs, this makes sense anyway, as the file ‘FILE.tar.gz’ is a gzipped version of the ‘FILE.tar’, which is presumably a tarball made from file ‘FILE’
This is why you would need to use something other than os.path.splitext for this, if what you need is the original filename, without .tar