visit
This is the second part of the series File management with python. We pick up from where we left last time Part 1, where we organized files according to the extension. So, let's get started.
Sometimes, organizing files might need just a bit more than knowing their extensions. For instance, take a directory where all the files are of the same type. Whether .pdf, .doc , .mp4 and so on. In this piece, we take our organization a little further. Say you have a folder with slides (.ppt ). In this case, you've just received a whole lot of lecture files, but they are not exactly easy to go through. So instead of having a whole slide having everything for the first lecture, they were broken down to a slide for a session. Our folder, in this case, is assumed to look as below.DataStructures/
|_Datastructuressession1Slide1.ppt
|_Datastructuressession1Slide2.ppt
|_Datastructuressession1Slide3.ppt
|_Datastructuressession2Slide3.ppt
|_Datastructuressession7Slide8.ppt
|_Datastructuressession9Slide2.ppt
... and so on
Remember how we generated random files in the previous article? We'll do the same thing, only this time, all the files will be of the same type. Have a look at that for a quick refresher. Our file looks much like the
create_random_files.py
.
#!/bin/python3
# create_lectures.py
import os
from pathlib import Path
sessions = [str(x) for x in range(1,21)] # create 20sessions
sessions = [str(0)+item if int(item) < 10 else item for item in sessions]
# Datastructuressession01Slide1.ppt
# get into the DataStructures directory
os.chdir('./DataStructures')
for item in sessions:
# create 20 slides for each session
for num in range(21):
file_to_create = f"Datastructuressession{item}Slide{num}.ppt"
Path(file_to_create).touch()
sessions = [str(0)+item if int(item) < 10 else item for item in sessions]
# clean_reading.py
#!/bin/python3
# move files to directories according to the file name pattern
import os
import shutil
# get into the Datastructures directory
os.chdir('./DataStructures')
# Datastructuressession01Slide1.ppt
for f in os.listdir("."):
folder_name = f[14:23]
# print(folder_name)
if not os.path.exists(folder_name):
os.mkdir(folder_name)
shutil.move(f, folder_name)
else:
shutil.move(f, folder_name)
folder_name = f[14:23]
We have counted the number of characters for the kind of name we want our folder to be named after. In this case, we broke down the string for the file name
Datastructuressession01Slide1.ppt
from where we get that the first s
for the session is character number 14
in the whole string while the last session count would be character number 23
which translating from our generated files would mean the last digit of the number 20
.Running this would get all our slides in their respective sessions quickly, and clean.There is a lot more that one would want to do, say let the program know which sessions are included without a manual key in of the character position, but more advanced tools exist for this especially under the UNIX environment. Feel free to do some scouting and find what works best for you. As a heads up, here's a cool GUI made with python. Paul did an awesome job on this. Questions? Do reach out in the comment section. As usual, all this code can be found at .Previously published at