Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python read file within tar archive

Tags:

python

tar

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!

like image 700
ashwin shanker Avatar asked Mar 17 '23 18:03

ashwin shanker


2 Answers

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()
like image 167
Stephen Lin Avatar answered Mar 28 '23 03:03

Stephen Lin


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())
like image 27
nathancahill Avatar answered Mar 28 '23 02:03

nathancahill