Feb-15-2021, 04:23 AM
I don't think many would agree with your opinion. Break breaks out of the loop in which it appears. Breaking out of all loops would be odd and make break difficult to use. I don't know of a language where break works differently.
Your problem is having two for loops. That is tricky in any language. I would write a function that returns True if a zip file contains a file that matches some pattern. Then your logic is much simpler. Note that none of this has been tested.
Your problem is having two for loops. That is tricky in any language. I would write a function that returns True if a zip file contains a file that matches some pattern. Then your logic is much simpler. Note that none of this has been tested.
def find_file_in_zip(zip_file_name, file_pattern): """Return True if file matching file_pattern found in zip file""" try: with ZipFile(zip_file_name, 'r') as zip_file: for filename in zip_file.namelist(): if pattern in filename: return True except OSError: pass; return False ndir_p = 'Some\\dir\\' files = [os.path.join(d_to_sacn, i) for i in os.listdir(d_to_sacn)] for zip_file in sorted(full_list, key=os.path.getmtime): zip_file = zip_file.strip() if find_file_in_zip(zip_file, 'Debug_TIU') parts = zipfile.split('\\') shutil.copy(zipfile, ndir_p + parts[-1]) break