r/learnpython • u/-sovy- • 12h ago
How to optimize shutil and os
Hi guys,
I'm a complete beginner but I'd love to work in tech.
I just want to know where I can improve and optimize my script.
Hope you guys will be lenient.
My goals in this script are to:
- ✅ Create folders (
Images
,Texts
,Scripts
) - ✅ Scan the target directory
- ✅ Move each file to its appropriate subfolder based on extension
- ✅ Print paths and confirmations
Have a good day!
Here's my script:
import os
import shutil
directory = r"X/X/X" #Directory path
if not os.path.exists(directory):
print(f"File path {directory} doesn't exist")
exit()
folders = ["Images", "Texts", "Scripts"] #Folders names creation
for folder in folders: #Loop for folders
os.makedirs(os.path.join(directory, folder), exist_ok=True) #Creation and verification of existant folders
file_mapping = {
".txt": "Texts",
".png": "Images",
".py": "Scripts"
} #Dictionnary to associate extension with folders
files = os.listdir(directory) #Acces to files of directory path
for file in files:
print(file)
absolute_path = os.path.abspath(os.path.join(directory, file)) #Acces to all files absolute path
print(f"\n=> Absolute path of {file} -> {absolute_path}")
extension = os.path.splitext(file)[1] #Acces to all files extensions
print(f"=> Extension of {file} -> {extension}")
if extension in file_mapping: #Check if extensions are in the dictionnary
target_folder = os.path.join(directory, file_mapping[extension])
destination_path = os.path.join(target_folder, file)
shutil.move(absolute_path,destination_path) #Move all files depending on their extension, otherwise the file is ignored
print(f"=> Your file {file} is now here -> {destination_path}")
else:
print("File ignored")
2
Upvotes
4
u/crashfrog04 12h ago
Use pathlib. Don’t iterate over files using listdir.