Rich55555
asked on
Python - Script to find if subfolder exists or not
I'm new to python.
I need to run a script which will delete files/directorys within a subfolder structure only if certain directorys exist.
Parent directory (p:\*fund*\) could be any directory name, for example:
p:\abc\letters\reject\
p:\def\letters\printed\
p:\ghi\letters\
So in above scenario, need to search the root directory (p:\*.*\letters\) for directorys called either reject or printed, then if exist delete the file contents of reject or printed.
I have many task like this one to do, hope that someone can provide me one example then I can tinker with it from then on.
Any help would be apprciated!!
Thanks in advance.
I need to run a script which will delete files/directorys within a subfolder structure only if certain directorys exist.
Parent directory (p:\*fund*\) could be any directory name, for example:
p:\abc\letters\reject\
p:\def\letters\printed\
p:\ghi\letters\
So in above scenario, need to search the root directory (p:\*.*\letters\) for directorys called either reject or printed, then if exist delete the file contents of reject or printed.
I have many task like this one to do, hope that someone can provide me one example then I can tinker with it from then on.
Any help would be apprciated!!
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Remove the # in last line from #shutil.rmtree(td) to actually delete the subfolders (else it only prints them to the console)
TEST:
Beggining with a dir structure like this (plus some files):
Directory of C:\area51
New Text Document (2).txt
New Text Document (3).txt
New Text Document.txt
Directory of C:\area51\printed
New Text Document (2).txt
New Text Document.txt
Directory of C:\area51\printed\a
New Text Document.txt
Directory of C:\area51\printed\b
Directory of C:\area51\printed\c
Directory of C:\area51\printed\reject
Directory of C:\area51\test
Directory of C:\area51\test\a
Directory of C:\area51\test\a\reject
Directory of C:\area51\test\b
Directory of C:\area51\test\printed
Directory of C:\area51\test\reject
Directory of C:\area51\test\reject\a
and The end result is this:
Directory of C:\area51
New Text Document (2).txt
New Text Document (3).txt
New Text Document.txt
Directory of C:\area51\test
Directory of C:\area51\test\a
Directory of C:\area51\test\b
Open in new window