Python Forum
How "continue" in another indentation?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How "continue" in another indentation?
#1
hey!

I have the following part of a code. It basically does lots of things with PDFs in folders. For reasons I needed to add a criteria wheer if a folder has more than 100 PDF it should be left out. The code looks like this:

#lots of code before this
for folder in subdirs:

	os.chdir(str(subdirs[subdirnumber + 1]))						#opens the subfolder
	pdflist = glob.glob("**/*.pdf", recursive=True)					#lists all the pdfs in the subfolder and its subfolders
	pdfstocheck = sum(1 for s in pdflist if 'apple' in s or 'orange' in s or 'cherry' in s or 'banana' in s or 'potato' in s or 'pear' in s or 
    'blackberry' in s or 'peanuts' in s or 'avocados' in s)

	if pdfstocheck > 100:
		f.write(str(os.getcwd()) + ',' + 'More than 100 PDFs to check. Please check the 793 reports manually.' + ',' + '\n')
		print("Now checking the folder: " + str(os.getcwd()))
		print("Number of PDFs in the folder: " + str(len(pdflist)))
		print("Number of PDFs to be checked: " + str(pdfstocheck))
		print("ERROR: Number of PDFs over 100. Please check the 793 reports manually.")
		print("")
		os.chdir(mainworkingdirectory)
		subdirnumber = subdirnumber + 1
		continue
			
	print("Now checking the folder: " + str(os.getcwd()))
	print("Number of PDFs in the folder: " + str(len(pdflist)))
	print("Number of PDFs to be checked: " + str(pdfstocheck))

	for file in pdflist:
#Lots of code after this
My problem is that I would need the code to go back to the "for folder in subdirs:" part and start checking the next folder but the continue statement returns it to "if pdfstocheck > 100:". This must be a very newbie question and I am sorry for it, I have been thinking about this for a while now. Is there a way for the code to go back to the "for folder in subdirs:" part after the "if" lines finish?
Reply
#2
Hi,
You could add a 'while not' to prevent the rest of the indentation if the pdfstocheck is less than 100.
Im fairly new to python so this might be wrong but you could try this:

#lots of code before this
for folder in subdirs:
	skipfolder = False
    os.chdir(str(subdirs[subdirnumber + 1]))                        #opens the subfolder
    pdflist = glob.glob("**/*.pdf", recursive=True)                 #lists all the pdfs in the subfolder and its subfolders
    pdfstocheck = sum(1 for s in pdflist if 'apple' in s or 'orange' in s or 'cherry' in s or 'banana' in s or 'potato' in s or 'pear' in s or 
    'blackberry' in s or 'peanuts' in s or 'avocados' in s)
 
    if pdfstocheck > 100:
        f.write(str(os.getcwd()) + ',' + 'More than 100 PDFs to check. Please check the 793 reports manually.' + ',' + '\n')
        print("Now checking the folder: " + str(os.getcwd()))
        print("Number of PDFs in the folder: " + str(len(pdflist)))
        print("Number of PDFs to be checked: " + str(pdfstocheck))
        print("ERROR: Number of PDFs over 100. Please check the 793 reports manually.")
        print("")
        os.chdir(mainworkingdirectory)
        subdirnumber = subdirnumber + 1
		skipfolder = True
        continue
	while not skipfolder:
	
		print("Now checking the folder: " + str(os.getcwd()))
		print("Number of PDFs in the folder: " + str(len(pdflist)))
		print("Number of PDFs to be checked: " + str(pdfstocheck))
 
		for file in pdflist:
#Lots of code after this
Reply
#3
Thank you for the reply Nuzvee. I can see the logic and it is a good idea but it does not work for some reason.
Reply
#4
would this work?

#lots of code before this
for folder in subdirs:
    skipfolder = False
    os.chdir(str(subdirs[subdirnumber + 1]))                        #opens the subfolder
    pdflist = glob.glob("**/*.pdf", recursive=True)                 #lists all the pdfs in the subfolder and its subfolders
    pdfstocheck = sum(1 for s in pdflist if 'apple' in s or 'orange' in s or 'cherry' in s or 'banana' in s or 'potato' in s or 'pear' in s or 
    'blackberry' in s or 'peanuts' in s or 'avocados' in s)
  
    if pdfstocheck > 100:
        f.write(str(os.getcwd()) + ',' + 'More than 100 PDFs to check. Please check the 793 reports manually.' + ',' + '\n')
        print("Now checking the folder: " + str(os.getcwd()))
        print("Number of PDFs in the folder: " + str(len(pdflist)))
        print("Number of PDFs to be checked: " + str(pdfstocheck))
        print("ERROR: Number of PDFs over 100. Please check the 793 reports manually.")
        print("")
        os.chdir(mainworkingdirectory)
        subdirnumber = subdirnumber + 1
 #       skipfolder = True
 #       continue
 
     else:
        print("Now checking the folder: " + str(os.getcwd()))
        print("Number of PDFs in the folder: " + str(len(pdflist)))
        print("Number of PDFs to be checked: " + str(pdfstocheck))
  
        for file in pdflist:
#Lots of code after this
Reply
#5
(May-03-2019, 12:17 PM)CaptainCsaba Wrote: My problem is that I would need the code to go back to the "for folder in subdirs:" part and start checking the next folder but the continue statement returns it to "if pdfstocheck > 100:". This must be a very newbie question and I am sorry for it, I have been thinking about this for a while now. Is there a way for the code to go back to the "for folder in subdirs:" part after the "if" lines finish?
Are you sure it skips? Try to visually debug by printing some information in the console:
#lots of code before this
i = 0;
for folder in subdirs:
	if (i == 0):
		pass
	else:
		print("Not skipping.")
 
    os.chdir(str(subdirs[subdirnumber + 1]))                        #opens the subfolder
    pdflist = glob.glob("**/*.pdf", recursive=True)                 #lists all the pdfs in the subfolder and its subfolders
    pdfstocheck = sum(1 for s in pdflist if 'apple' in s or 'orange' in s or 'cherry' in s or 'banana' in s or 'potato' in s or 'pear' in s or 
    'blackberry' in s or 'peanuts' in s or 'avocados' in s)
 
    if pdfstocheck > 100:
		if (i == 0):
			i = 1;
        f.write(str(os.getcwd()) + ',' + 'More than 100 PDFs to check. Please check the 793 reports manually.' + ',' + '\n')
        print("Now checking the folder: " + str(os.getcwd()))
        print("Number of PDFs in the folder: " + str(len(pdflist)))
        print("Number of PDFs to be checked: " + str(pdfstocheck))
        print("ERROR: Number of PDFs over 100. Please check the 793 reports manually.")
        print("")
        os.chdir(mainworkingdirectory)
        subdirnumber = subdirnumber + 1
        continue
             
    print("Now checking the folder: " + str(os.getcwd()))
    print("Number of PDFs in the folder: " + str(len(pdflist)))
    print("Number of PDFs to be checked: " + str(pdfstocheck))
 
    for file in pdflist:
#Lots of code after this
Reply
#6
I don't know why but it worked this way. Thank you!
Reply
#7
Maybe by this way:
#lots of code before this
for folder in subdirs:
 
    os.chdir(str(subdirs[subdirnumber + 1]))                        #opens the subfolder
    pdflist = glob.glob("**/*.pdf", recursive=True)                 #lists all the pdfs in the subfolder and its subfolders
    pdfstocheck = sum(1 for s in pdflist if 'apple' in s or 'orange' in s or 'cherry' in s or 'banana' in s or 'potato' in s or 'pear' in s or 
    'blackberry' in s or 'peanuts' in s or 'avocados' in s)
 
    if pdfstocheck > 100:
        f.write(str(os.getcwd()) + ',' + 'More than 100 PDFs to check. Please check the 793 reports manually.' + ',' + '\n')
        print("Now checking the folder: " + str(os.getcwd()))
        print("Number of PDFs in the folder: " + str(len(pdflist)))
        print("Number of PDFs to be checked: " + str(pdfstocheck))
        print("ERROR: Number of PDFs over 100. Please check the 793 reports manually.")
        print("")
        os.chdir(mainworkingdirectory)
        subdirnumber = subdirnumber + 1
    else:             
        print("Now checking the folder: " + str(os.getcwd()))
        print("Number of PDFs in the folder: " + str(len(pdflist)))
        print("Number of PDFs to be checked: " + str(pdfstocheck))
 
        for file in pdflist:
        #Lots of code after this
Reply


Forum Jump:

User Panel Messages

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