I am writing program which fetch extension of filename from string than download it and extract it, I am able to do it with Tar.gz and zip type of compression but while extracting Bz2 type of file i am able to read file and transfer data to new file in same directory but what i would like is to create folder like zipfile and tarfile using something like 'ExtractAll" and extract file in it, but i am unable can anyone help
def bz2_download(*args):
mystring_zip = ' '.join(args)
print('{} Bz2 file Download Started!!!'.format(mystring_zip.split('/')[-1].split('.')[-2]))
r = requests.get(mystring_zip)
filename = mystring_zip.split('/')[-1] # this will take only -1 splitted part of the url
with open(filename, 'wb') as output_file:
output_file.write(r.content)
print('{} Bz2 file Download Completed!!!'.format(mystring_zip.split('/')[-1].split('.')[-2]))
path_zip = mystring_zip.split('/')[-1].split('.')[0]
print(path_zip, '+++++++++++++++++++++++++++++++++++++++++++++++++++++++')
extract_dir = path_zip
print('--------------------------{} extraction Started----------------------------------'.format(extract_dir))
os.mkdir(path_zip)
with open(path_zip, 'wb') as new_file, open(filename, 'rb') as file:
decompressor = _bz2.BZ2Decompressor()
for data in iter(lambda: file.read(100 * 1024), b''):
a = new_file.write(decompressor.decompress(data))
print(a, '1111111111111111111111111111111111111111111')
if extract_dir:
print('---------------------------{} extraction completed----------------------------------'.format(extract_dir))
else:
print('---------------------------{} extraction Failed----------------------------------'.format(extract_dir))
Here is my Tar.gz code which download the file and extract it to a folder, want to do something similar with bzip2 file
def tar_download(*args):
mystring_tar = ' '.join(args)
print('{} Tar.gz file Download Started!!!'.format(mystring_tar.split('/')[-1].split('.')[0]))
r = requests.get(mystring_tar)
filename = mystring_tar.split('/')[-1]
with open(filename, 'wb') as output_file:
output_file.write(r.content)
print(' {} Tar.gz file Download Completed!!!'.format(mystring_tar.split('/')[-1].split('.')[0]))
path_tar = mystring_tar.split('/')[-1].split('.')[0]
extract_dir = path_tar
print('---------------------------{} extraction Started----------------------------------'.format(extract_dir))
thetarfile = tarfile.open(filename, mode="r|gz")
thetarfile.extractall(extract_dir)
if extract_dir:
print('---------------------------{} extraction Complete----------------------------------'.format(extract_dir))
else:
print('---------------------------{} extraction Failed----------------------------------'.format(extract_dir))