Feb-14-2021, 01:03 AM
Hi,
I'm having a hard
time to 'break' from a 'for loop' in a right place.
I'm scanning a directory for the ZIP files.
And test them (from newest to the oldest) for a specific file, and if a file is in a ZIP file I want to copy that ZIP
to a different location.
The script I got so far does this:
1. Scans a directory and makes a list of ZIP files starting from the newest to the oldest.
2. Tests the Zip files from the list for a specific file (File starts with the word Debug_).
3. prints all the ZIP files with the specific file name.
I suppose to have one Zip file (latest ZIP file) from each directory but I cannot "break" the loop.
Code:
I'm having a hard
time to 'break' from a 'for loop' in a right place.I'm scanning a directory for the ZIP files.
And test them (from newest to the oldest) for a specific file, and if a file is in a ZIP file I want to copy that ZIP
to a different location.
The script I got so far does this:
1. Scans a directory and makes a list of ZIP files starting from the newest to the oldest.
2. Tests the Zip files from the list for a specific file (File starts with the word Debug_).
3. prints all the ZIP files with the specific file name.
I suppose to have one Zip file (latest ZIP file) from each directory but I cannot "break" the loop.
Code:
ndir_p = 'Some\\dir\\'
lst_OF_files = os.listdir(d_to_sacn) ## Directory with ZIP files ##
full_list = [os.path.join(d_to_sacn,i) for i in lst_OF_files]
time_sorted_lst = sorted(full_list, key=os.path.getmtime) ## ZIP files Sorted started from Newest file ##
try :
for ls in time_sorted_lst :
ls=ls.strip()
#print ("______________ ",ls)
with ZipFile (ls, 'r') as zip_file :
file_name_list = zip_file.namelist ()
for file_name in file_name_list :
if 'Debug_TIU' in file_name : ## Looking for a specific file Name ##
print ("IN ZIP -->> ",ls)
ls_p = ls.split('\\')
dst = ndir_p+ls_p[-1] ## Creating a destination directory string ##
print ("ZIP Name ---------->> ", ls_p[-1])
print ("Destination String -->> ",dst)
shutil.copy(ls,dst)
break
except OSError as er :
print ("ERROR Opening ZIP file ",file_name)
continue Thank you in advance!
