-1

I am using the tarfile package for python.

Tar = tarfile.open("filename.tar.gz" mode='w:gz')

NOTE: tarfile.open("filename.**TAR.GZ**" mode='w:gz') is creating two levels of compression. no idea if it is a bug in the package

It is creating "filename.tar.gz"; however, for Unix constraints, we need "filename.**TAR.GZ**"(please note that extension is in uppercase.)

Could you please help me to create filename.TAR.GZ? I can use other packages than tarfile if available for my need.

Axe319
  • 4,255
  • 3
  • 15
  • 31
  • filename.TAR.GZ and filename.tar.gz is the same; if you really want uppercase: `from shutil import move; move('filename.tar.gz','filename.TAR.GZ')` – Ningrong Ye Oct 09 '20 at 10:16
  • Thank you very much Ningrong. It worked for me. However, using 7 zip Manager, I can see under filename.TAR.GZ, filename.tar (where the extension is in smaller case). – Saumya Bhattacharya Oct 12 '20 at 10:57

1 Answers1

0

Is this what you want?

import tarfile

file_to_add = "data.csv"
tar_archive = "file.TAR.GZ"

out = tarfile.open(tar_archive, "w:gz")
try:
    print(f"Adding {file_to_add}")
    out.add(file_to_add)
finally:
    print(f"Closing tar archive: {tar_archive}")
    out.close()

print(f"Contents of archive: {tar_archive}")
t = tarfile.open(tar_archive)
for member in t.getmembers():
    print(member.name)

Output

Adding data.csv
Closing tar archive: file.TAR.GZ
Contents of archive: file.TAR.GZ
data.csv

And if I do ls on the working dir, here's what's there:

enter image description here

baduker
  • 19,152
  • 9
  • 33
  • 56
  • Thank you for your comment. However, 'w' is used for "open for uncompressed writing". Hence GunZip/unix decompression might not work. Try "tar -xvf filename.TAR.GZ" in unix terminal – Saumya Bhattacharya Oct 12 '20 at 11:03
  • It was a typo on my side. I've fixed it. Thanks! No this should work just fine. – baduker Oct 12 '20 at 11:25