I'm trying to extract and print the list of unziped files. If the files are already unzipped then i'm printing a message saying file is already extracted. But the problem here is, even if the files are already extracted it is printing as "extracting file1". See the below code for more details.
import re
import os
import os.path
local_download_directory = "/Users/TargetData/zip"
Target_directory_to_extract = "/Users/TargetData/unzip"
def extract_zip_files(local_download_directory,Target_directory_to_extract):
zipFiles = [f for f in os.listdir(local_download_directory) if re.match(r'.*\.tar\.gz', f)]
for f_name in zipFiles:
if os.path.exists(Target_directory_to_extract+"/"+f_name[:-len('.tar.gz')]) and os.access(Target_directory_to_extract+"/"+f_name[:-len('.tar.gz')], os.R_OK):
print ('File {} already exists!'.format(f_name))
else:
print ('Extracted file {}'.format(f_name))
f_name_with_path = os.path.join(local_download_directory, f_name)
os.system('mkdir -p {} && tar vxzf {} -C {}'.format(Target_directory_to_extract, f_name_with_path, Target_directory_to_extract))
return os.EX_OK
extract_zip_files(local_download_directory,Target_directory_to_extract)
Output:
- Extracted file 2314590000.tar.gz
- File 2314590001.tar.gz already exists!
- File 2317590000.tar.gz already exists!
- File 2317590001.tar.gz already exists!
- File 2250050000.tar.gz already exists!
- File 2034000000.tar.gz already exists!
- File 2129200000.tar.gz already exists!
Now my question is why it is printing the first line in output when all the files are already extracted.i.e. "Extracted file 2314590000.tar.gz". And why that file is not displayed "File 2314590000.tar.gz already exists!"
Thank you for your help, comments, suggestion in advance!
Aucun commentaire:
Enregistrer un commentaire