Python Forum
Cannot 'break' from a "for" loop in a right place
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot 'break' from a "for" loop in a right place
#5
I got upset, you are right of course, I just do not understand the Python Loops...
Anyway, as I said, my task is to:
1. Scan remote machines (specific folders) for the ZIP files and Copy 1 (one) latest ZIP file but only if it has a file
Named "DEbug_xxx.log
2. Copy 1 (one) latest ZIP file that has "Debug_xxx.log" from each remote machine to a server.

The part of the script I need help with is the one I have already posted.
It accesses the remote machines and scans/makes a list of the ZIP files (newest to oldest)
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 ##


Then it finds all the ZIPs with the file "debug_xxx.log" inside the ZIPs on each remote machine and
Copies all the ZIPS to different directories on my serve (actually on my server these directories named after the remote machine names), see below :
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
But I need to copy only a 1 (one) ZIp file from each remote machine,
Not all as I do now.
I need to put a 'break' in the loop,

I tried to move the God damn 'break' all over the script and still cannot achieve my goal.
that is copying 1 (one) Zip file that has a 'Debug_xxx.log' inside the ZIP from each remote directory.
I hope it is clear but maybe not.
And again, I really appreciate your help!
Reply


Messages In This Thread
RE: Cannot 'break' from a "for" loop in a right place - by tester_V - Feb-14-2021, 07:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  place 2 windows exactly above each other janeik 3 1,023 Jul-23-2023, 03:12 AM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 1,082 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  How to break out of a for loop on button press? philipbergwerf 6 1,913 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  break out of for loop? User3000 3 1,551 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,808 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  tkinter control break a while loop samtal 0 2,475 Apr-29-2021, 08:26 AM
Last Post: samtal
  How to break a loop in this case? Blainexi 10 7,500 Sep-24-2020, 04:06 PM
Last Post: Blainexi
  how to break the loop? bntayfur 8 3,173 Jun-07-2020, 11:07 PM
Last Post: bntayfur
  Where should I place GPIO.cleanup() shallanq 2 2,251 Apr-11-2020, 05:02 AM
Last Post: shallanq
  break for loop Agusben 1 1,987 Apr-01-2020, 05:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020