This type of question has been asked before, but I can't seem to get exactly what I want through the help I've found.
This question has an answer by user Iker, where the user gives code that does work exactly as intended: it removes all the files and directories from a folder, but not the parent folder itself.
I want to tweak this further by deleting all the files from a parent directory, but not the parent directory, and I want to exclude a folder within the directory.
The code I am using is:
import os
import shutil
files = '[the path to my folder]'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
I have tried adding an "If" statement after the "for" statement that basically says:
if files != keep
and I have a variable "keep" pointing to a folder in the parent file called "keep," so that this script will delete everything except the parent directory and the directory called "keep" within the parent. However, after adding that, the code does not work.
Here is the exact code I had, with the if statement that breaks the code:
import os
import shutil
files = '[the path to the parent folder'
keep = '[the path to the "keep" folder]'
for root, dirs, files in os.walk(files):
for f in files:
if files != keep:
os.unlink(os.path.join(root, f))
for d in dirs:
if files != keep:
shutil.rmtree(os.path.join(root, d))
I'm sure what I'm doing is very obvious, but it is not obvious to me, so any help will be appreciated.
Thanks!
EDIT: Based on Ben's answer below, here is the code that worked for me:
import os
import shutil
root_dir = r'[path to directory]' # Directory to scan/delete
keep = 'keep' # name of file in directory to not be deleted
for root, dirs, files in os.walk(root_dir):
for name in files:
# make sure what you want to keep isn't in the full filename
if (keep not in root and keep not in name):
os.unlink(os.path.join(root, name)) # Deletes files not in 'keep' folder
for name in dirs:
if (keep not in root and keep not in name):
shutil.rmtree(os.path.join(root, name)) # Deletes directories not in 'keep' folder