Python Extract gz files with and honor original filenames and file extensions

0 votes

Under a folder, I have many .gz files and within these gz files some are .txt, some are .csv, some are .xml, or some other extensions.

E.g. gz (the original/compressed file in()) files in the folder will be

C:\Xiang\filename1.txt.gz (filename1.txt)
C:\Xiang\filename2.txt.gz (filename2.txt)
C:\Xiang\some_prefix_filename3.txt.gz (filename3.txt)
...
C:\Xiang\xmlfile1.xml_some_postfix.gz   (xmlfile1.xml)
C:\Xiang\yyyymmddxmlfile2.xml.gz       (xmlfile2.xml)
...
C:\Xiang\someotherName.csv.gz            (someotherName.csv)
C:\Xiang\possiblePrefixsomeotherfile1.someotherExtension.gz (someotherfile1.someotherExtension)
C:\Xiang\someotherfile2.someotherExtensionPossiblePostfix.gz (someotherfile2.someotherExtension)
...

How could I simply up-zip all the .gz files in Python on Windows 10 under the folder C:\Xiang and save into folder C:\UnZipGz, honor the original filenames, with the result as follows:

C:\UnZipGz\filename1.txt
C:\UnZipGz\filename2.txt
C:\UnZipGz\filename3.txt
...
C:\UnZipGz\xmlfile1.xml.
C:\UnZipGz\xmlfile2.xml.
...
C:\UnZipGz\someotherName.csv.
C:\UnZipGz\someotherfile1.someotherExtension
C:\UnZipGz\someotherfile2.someotherExtension
...

Generally, the gz files naming convention are consistent with the filenames of the files inside, but it is not always the case. Somehow, renaming to the some .gz files happened in the past. Now the gz file names does not necessarily match with the filenames of the file in gz files.

How could I extract all the gz files and keep the original file filenames and extensions. I.e, regardless how the gz files are named, when extracting gz files, only save the un-zip files in the original format as

filename.fileExtension

into the C:\UnZipGz folder.

Apr 29, 2022 in Linux Administration by Edureka
• 13,690 points

edited 4 days ago 6 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP