PC Cleaner — Python
2 min readJul 1, 2021
Description:
- Let’s look at a Python script that will place all of your files in the appropriate folder.
- This script will first list all of the files and place them in folders based on their extensions, such as “.pdf” which will be placed in the PDFs folder, and so on for all of the files.
- Finally, all of the files will be placed in their proper directories.
Procedure to follow:
- Import OS module
- Using the chdir function, change the location where the files will be imported.
- List all of the files using the listdir function, and then delete any files that don’t have an extension like any folder.
- Run a loop for all of the files, putting them in the appropriate folders based on their extensions.
- Finally, each file will be placed in its own folder.
Sample Code:
# Import the module
import os# Function for creating new folder
def create_folder(folder):
if not os.path.exists(folder):
os.mkdir(folder) # Make a folder with the name given by the user# Function to move files inside the folder
def move_files(folder_name, files):
for file in files:
# Moving all the files inside the folder
os.replace(file, f"{folder_name}/{file}")
# Driver function
if __name__ == "__main__":
path = r"C:\Users\Dell\Downloads\All"
os.chdir(path) # Jump to the directory where all the files present
all_files = os.listdir() # List all the files of the directory
for i in range(len(all_files)):
try:
# Check only for the files not for the folders
all_files[i] = "."+all_files[i].split(".")[1]
except:
pass # If it'a a folder then nothing to do
# List all the files which contains extension, not folders because folders don;t have any extension
all_files = [file for file in all_files if "." in file] for file in all_files: # Loop for arranging all the files in a separate folders
folder_name = f"{file[1:].upper()} Files"
create_folder(folder_name)
files = [fil for fil in os.listdir() if os.path.splitext(fil)
[1] == file]
move_files(folder_name, files)
Output:
Before:
After:
Follow me on Twitter (@triposat)
Thanks!