Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import shutil
- def process_directory(directory):
- """
- Process the specified directory.
- - Delete layer 1 files.
- - If any layer 2 folder is named "backup", move its files to layer 1 and delete the remaining layer 2 directories.
- """
- for root, dirs, files in os.walk(directory):
- # Process layer 1 files
- if root != directory:
- for filename in files:
- file_path = os.path.join(root, filename)
- os.remove(file_path)
- # Process layer 2 folders
- for subdir in dirs:
- subdir_path = os.path.join(root, subdir)
- if os.path.isdir(subdir_path):
- # Check if the folder is named "backup"
- if subdir.lower() == "backup":
- # Move files from layer 2 to layer 1
- for subfile in os.listdir(subdir_path):
- subfile_path = os.path.join(subdir_path, subfile)
- if os.path.isfile(subfile_path):
- shutil.move(subfile_path, os.path.join(root, subfile))
- # Delete the remaining layer 2 directory
- shutil.rmtree(subdir_path)
- else:
- # Check if the folder contains layer 2 files
- layer2_files = [f for f in os.listdir(subdir_path) if os.path.isfile(os.path.join(subdir_path, f))]
- if layer2_files:
- # Do not delete the layer 2 directory
- pass
- # Detect the current directory
- current_directory = os.path.dirname(os.path.abspath(__file__))
- # Process the current directory
- process_directory(current_directory)
Add Comment
Please, Sign In to add comment