I have a file:"docs.tar.gz".The tar file has 4 files inside of which the fourth file is "docs.json" which is what I need.Im able to view the contents of the tar file using:
import tarfile
tar=tarfile.open("docs.tar.gz")
tar.getmembers()
How would I read the fourth file -the json file that I need?..Im unable to proceed after extracting the contents.Thanks!
This one will work too.
import tarfile
tar = tarfile.open("docs.tar.gz")
files = tar.getmembers()
f = tar.extractfile(files[0]) # if your docs.json is in the 0th position
f.readlines()
Try this:
import tarfile
tar = tarfile.open("docs.tar.gz")
f = tar.extractfile("docs.json")
# do something like f.read()
# since your file is json, you'll probably want to do this:
import json
json.loads(f.read())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With