Skip to content

Commit a991b79

Browse files
Added README,script script_dirs.py and output images
1 parent f2b961c commit a991b79

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Script to organise files according to their extensions
2+
3+
4+
- The script on running would ask for a Path directory
5+
- If no Path is entered then it will take current working directory as the path.
6+
- On running this script the files in a directory will be put into different subdirectories as per their extensions.
7+
8+
## Setup instructions
9+
10+
- The script doesn't require any installations and is not dependent on any OS.
11+
- Just running the script and giving the desired input will do.
12+
13+
14+
## Output
15+
16+
![img1]("https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/script-to-organise-files-according-to-their-extensions/img/1.png")
17+
18+
19+
![img2]("https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/script-to-organise-files-according-to-their-extensions/img/2.png")
20+
21+
22+
23+
![img3]("https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/script-to-organise-files-according-to-their-extensions/img/2.png")
24+
25+
## Author(s)
26+
27+
Author : [Mohta Rahul Suresh](https://github.com/Rahul555-droid)
29 KB
Loading
7.04 KB
Loading
56.1 KB
Loading
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
from pathlib import Path
3+
import sys
4+
5+
# Taking input
6+
print_string = """
7+
Type Path of the directory
8+
OR
9+
Press enter for running the script on current directory:
10+
OR
11+
Type quit
12+
"""
13+
print(print_string+"\n\n")
14+
input_path = input("Input:")
15+
print("\n\n")
16+
17+
# Script will terminate if input is 'quit'
18+
if input_path == "quit":
19+
sys.exit(1)
20+
21+
# If nothing is entered then current working directory will be taken as the input path
22+
if input_path == "":
23+
input_path = os.getcwd()
24+
25+
input_path = Path(input_path)
26+
27+
# Changing the working directory to input path
28+
os.chdir(input_path)
29+
30+
# Creates a dictionary "dic" with key,value pairs where key is extension and value is no. of files with that extension
31+
dic = {}
32+
for file in os.listdir(os.getcwd()):
33+
if os.path.isfile(file):
34+
extension = file.split(".")[-1]
35+
dic[extension] = dic.get(extension, 0)+1
36+
37+
for key in dic:
38+
print(f"There are {dic[key]} files file with extension {key}")
39+
print("\n\n")
40+
41+
# assigning a variable named current Path of current working directory just for simplicity.
42+
# could have used input_path too
43+
current = Path(os.getcwd())
44+
45+
'''
46+
When this script would run the structure of the current directory would change.Hence,
47+
we are assigning list_dir variable the files and dirs in current working directory which the script would modify
48+
'''
49+
list_dir = os.listdir(current)
50+
51+
52+
# keys of dic are extensions of the file
53+
for key in dic:
54+
# try except block for making directory if it doesn't exists already
55+
try:
56+
os.mkdir(key)
57+
except:
58+
print(f"directory named {key} already exists so it won't be overwrited \n")
59+
60+
# goes through the files in list_dir
61+
# we are not using os.listdir() as the directory structure will change during the execution
62+
for file in list_dir:
63+
if file.split(".")[-1] == key and os.path.isfile(file):
64+
# prints absolute path of the file
65+
print(os.path.abspath(file))
66+
# Renames the path of the file or moves the file in to the newly created directory
67+
Path.rename(Path(os.path.abspath(file)), current /
68+
Path("./{}/".format(key)+file))
69+
70+
# This block just prints a note and the current structure of the directory
71+
72+
print("\n Script has organised files as per their extensions into different directories! \n")
73+
for file in os.listdir(os.getcwd()):
74+
if not(os.path.isfile(file)):
75+
print(file)

0 commit comments

Comments
 (0)