I have a tar file which has number of folders within it. In each folder there are number of image files. I need to write a python script which will read each image file and perform some action on the image (ex: thresholding etc.,) and save the image file in the directory I specified. This process need to be done without un-tarring the tar file.
t = tarfile.open('example.tar', 'r')
for member in t.getmembers():
f = t.extractfile(member)
While I am trying to print f
, it's returning None
type. What am I doing wrong?
Simply use this function.
import tarfile
#simple function to extract the train data
#tar_file : the path to the .tar file
#path : the path where it will be extracted
def extract(tar_file, path):
opened_tar = tarfile.open(tar_file)
if tarfile.is_tarfile(tar_file):
opened_tar.extractall(path)
else:
print("The tar file you entered is not a tar file")
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