The best (most "pythonic") way to temporarily unzip a file

I need to temporarily create an unpacked version of some files. I saw people doing zcat somefile.gz > /tmp/somefile in bash, so I made this simple function in python:

 from subprocess import check_call def unzipto(zipfile, tmpfile): with open(tmpfile, 'wb') as tf: check_call(['zcat', zipfile], stdout=tf) 

But the use of zcat and check_call seems hacked to me, and I was wondering if there is a more "puffic" way to do this.

thanks for the help

+6
source share
2 answers

gzip.open(zipfile).read() will provide you the contents of the file on one line.

 with open(tmpfile, "wb") as tmp: shutil.copyfileobj(gzip.open(zipfile), tmp) 

puts the contents into a temporary file.

+16
source

Do you consider using zlib or gzip or tarfile .

+6
source

Source: https://habr.com/ru/post/903620/


All Articles