Apologies if this question has been asked before, but I wasn't able to find a similar question and hoped SO would recommend me one as I typed.
Scenario: Manually created folders with subfolder and files. At each level of folders I want to perform a particular function. Rather than raise an exception each time an error encountered, I'd like to print them out and continue the loop so I have a complete list of errors at the end.
Here is a MWE using simplified functions of what my code looks like:
import os, stat
def folder_info(folder):
if len(folder) > 20:
raise Exception('Folder {} name too long'.format(folder))
print(folder)
subfolders = os.listdir(folder)
return len(folder), subfolders
raise_error = False
folders = [f for f in os.listdir() if os.path.isdir(f)]
for folder in folders:
try:
length, subfolders = folder_info(folder)
except Exception as error:
if raise_error: raise
print('Error: {}'.format(error))
continue
for subfolder in subfolders:
subfolder = os.path.join(folder, subfolder)
try:
length, subfolders = folder_info(subfolder)
except Exception as error:
if raise_error: raise
print('Error: {}'.format(error))
continue
Ideally I'd like something where I don't have to type out a try
and except
statement for each nested loop. What I'm hoping for:
for folder in folders:
arg1, arg2 = exceptionwrapper(myfunction1(folder), raise_error)
for subfolder in arg2:
subfolder = os.path.join(folder, subfolder)
arg1, arg2, arg3 = exceptionwrapper(myfunction2(subfolder), raise_error)
for subsubfolder in arg3:
arg1, arg2 = exceptionwrapper(myfunction3(subsubfolders), raise_error)
I've tried following solutions with decorator functions but I wasn't able to pass in continue
or figure out where it should go. This is my best attempt and I have a syntax error trying to pass in continue
:
def exceptionwrapper(function, raise_error, after_error: function = continue):
def decorator(func):
def new_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as error:
if raise_error: raise
print('Error: {}'.format(error))
return after_error
return new_func
return decorator
Any help would be appreciated!
Your entire code can be written in few lines in a Pythonic
way, if you could have used os.walk .
import os
for root, folders, files in os.walk(os.getcwd()):
for folder in folders:
print('dir is: ', folder)
try:
if len(folder) > 20:
raise Exception('Folder {} name too long'.format(folder))
except Exception as error_message:
print('Error in folder: {} due to: {}'.format(folder, error_message))
As you have specified in different folder level, you have to call different functions, you may try like below:
import os
curr_folder = os.getcwd()
for root, folders, files in os.walk(curr_folder):
for folder in folders:
print('dir is: ', folder)
folder_level = root[len(curr_folder)+1:].count(os.sep)
print('level is: ', folder_level)
if folder_level == 0:
# call myfunction0
pass
elif folder_level == 1:
# call myfunction1
pass
elif folder_level == 2:
# call myfunction2
pass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.