Skip to content

Commit 6986b7d

Browse files
Merge pull request prathimacode-hub#174 from Iamtripathisatyam/main
PC_Cleaner
2 parents 52c5b30 + ea747b2 commit 6986b7d

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed
Loading
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
![](http://ForTheBadge.com/images/badges/made-with-python.svg)
2+
![](https://forthebadge.com/images/badges/built-by-developers.svg)</br>
3+
[![Prettier](https://img.shields.io/badge/Code%20Style-Prettier-red.svg)](https://github.com/prettier/prettier)
4+
![License](https://img.shields.io/badge/License-MIT-red.svg)</br>
5+
6+
## Description:
7+
- Let's [**look**](https://github.com/Iamtripathisatyam/Awesome_Python_Scripts/blob/main/BasicPythonScripts/PC%20Cleaner/pc_cleaner.py) at a Python script that will place all of your files in the appropriate folder.
8+
- 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.
9+
- Finally, all of the files will be placed in their proper directories.
10+
11+
## Procedure to follow:
12+
- Import **OS** module
13+
- Using the **chdir** function, change the location where the files will be imported.
14+
- List all of the files using the **listdir** function, and then delete any files that don't have an extension like any folder.
15+
- Run a loop for all of the files, putting them in the appropriate folders based on their extensions.
16+
- Finally, each file will be placed in its own folder.
17+
18+
## Sample Output:
19+
20+
### Before:
21+
22+
![hey](https://github.com/Iamtripathisatyam/Awesome_Python_Scripts/blob/main/BasicPythonScripts/PC%20Cleaner/Images/output_1.jpg)
23+
24+
### After:
25+
26+
![hey](https://github.com/Iamtripathisatyam/Awesome_Python_Scripts/blob/main/BasicPythonScripts/PC%20Cleaner/Images/output_2.jpg)
27+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Import the module
2+
import os
3+
4+
# Function for creating new folder
5+
6+
7+
def create_folder(folder):
8+
if not os.path.exists(folder):
9+
os.mkdir(folder) # Make a folder with the name given by the user
10+
11+
# Function to move files inside the folder
12+
13+
14+
def move_files(folder_name, files):
15+
for file in files:
16+
# Moving all the files inside the folder
17+
os.replace(file, f"{folder_name}/{file}")
18+
19+
20+
# Driver function
21+
if __name__ == "__main__":
22+
path = r"C:\Users\Dell\Downloads\All"
23+
os.chdir(path) # Jump to the directory where all the files present
24+
all_files = os.listdir() # List all the files of the directory
25+
for i in range(len(all_files)):
26+
try:
27+
# Check only for the files not for the folders
28+
all_files[i] = "."+all_files[i].split(".")[1]
29+
except:
30+
pass # If it'a a folder then nothing to do
31+
# List all the files which contains extension, not folders because folders don;t have any extension
32+
all_files = [file for file in all_files if "." in file]
33+
34+
for file in all_files: # Loop for arranging all the files in a separate folders
35+
folder_name = f"{file[1:].upper()} Files"
36+
create_folder(folder_name)
37+
files = [fil for fil in os.listdir() if os.path.splitext(fil)
38+
[1] == file]
39+
move_files(folder_name, files)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import os

0 commit comments

Comments
 (0)