-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorgnize.py
More file actions
24 lines (18 loc) · 761 Bytes
/
Copy pathorgnize.py
File metadata and controls
24 lines (18 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
import shutil
def organize_files_by_extension(src_dir):
# Iterate over all the entries
for filename in os.listdir(src_dir):
# If the entry is a file
if os.path.isfile(os.path.join(src_dir, filename)):
# Get file extension
file_ext = filename.split('.')[-1]
# Create a new directory if it doesn't exist
new_dir = os.path.join(src_dir, file_ext)
if not os.path.exists(new_dir):
os.makedirs(new_dir)
# Move the file
shutil.move(os.path.join(src_dir, filename),
os.path.join(new_dir, filename))
src_dir = '/path/to/your/directory' # Specify your source directory
organize_files_by_extension(src_dir)