Jan-07-2020, 09:28 AM
Hello everyone,
let's say I want to create a 'utils' package with several functions
working on files and folders. I want to do it once properly so I can use
them in all my personal projects without feeling to re invent the wheel
each time.
I also want to learn 2 items by the way:
- exception handling, already read a lot about EAFP vs LBYL, I think the
following function suits well to EAFP.
- unit tests with pytest tool, it seems simple enough to start with.
My function is:
Regarding the function it self:
do you think it is worth handling each possible exception or should I just
handle IsADirectoryError for folder removing and all the other one in a unique
one?
What would be the interest to raise an exception in that case to be caught by the caller?
Are there some exceptions I didn't handle and that could possibly occur?
Regarding the testing:
I need to test whether a file or a folder is well removed and the exception are
well handled:
- file exists and permissions are ok => file is removed
- folder exists and permissions are ok => folder is removed
- neither file or folder exist => FileNOtFoundError
- permissions issues => PermissionError
My main concern is to create the conditions of the test (files/folders to be
removed with good permissions) and to remove them in a clean way once test is over
or has been interrupted.
What is the skeletton of those steps?
Sorry for this long post.
Thanks by adavnce for your answers.
let's say I want to create a 'utils' package with several functions
working on files and folders. I want to do it once properly so I can use
them in all my personal projects without feeling to re invent the wheel
each time.
I also want to learn 2 items by the way:
- exception handling, already read a lot about EAFP vs LBYL, I think the
following function suits well to EAFP.
- unit tests with pytest tool, it seems simple enough to start with.
My function is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def removeanything(src): """ remove files or folders """ try : os.remove(src) print ( 'File is removed' ) except IsADirectoryError: shutil.rmtree(src) print ( 'Folder is removed' ) except FileNotFoundError: print ( 'File/folder is not existing' ) except PermissionError: print ( 'Not allowed to suppress this file' ) |
Regarding the function it self:
do you think it is worth handling each possible exception or should I just
handle IsADirectoryError for folder removing and all the other one in a unique
one?
What would be the interest to raise an exception in that case to be caught by the caller?
Are there some exceptions I didn't handle and that could possibly occur?
Regarding the testing:
I need to test whether a file or a folder is well removed and the exception are
well handled:
- file exists and permissions are ok => file is removed
- folder exists and permissions are ok => folder is removed
- neither file or folder exist => FileNOtFoundError
- permissions issues => PermissionError
My main concern is to create the conditions of the test (files/folders to be
removed with good permissions) and to remove them in a clean way once test is over
or has been interrupted.
What is the skeletton of those steps?
Sorry for this long post.
Thanks by adavnce for your answers.