- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Build a CLI File Organizer in Python
In this Code Lab, you will organize diverse company data—images, audios, videos, and scripts—using the CLI. You will create distinct directories, sort files, address duplicates, archive old files, and apply encryption/decryption for data security. By the end of this lab, you will have enhanced your skills in organizing varies data formats.
Lab Info
Table of Contents
-
Challenge
Introduction
Hello, fellow managers! Your primary goal in this lab is to enhance data management, streamline access, and ensure the highest level of data security by building a CLI File Organizer in Python.
Lab Scope
You will be working with various data types:
- Images (
.PNG,.JPG, and.GIF) - Audios (
.M4A, and.MP3) - Videos (
.MP4) - Scripts (
.JSON)
Throughout this lab, you will complete steps the following steps to add functionality to your CLI File Organizer:
- Create sub-directories based on file types.
- Sort files by their sizes.
- Handle duplicate file names.
- Archive files that are older than one year.
- Apply encryption/decryption to secure the files.
Lab Structure
You are working in the
/home/ps-user/workspacedirectory, where you have four directories:srcdirectory holds five Python scripts:starter.py,images.py,audios.py,videos.py, andscripts.py(one for each step).staticdirectory holds all types of files that you must use while completing each step.datadirectory holds different files with the same data types. You must first validate all the tests and later use the Terminal to organize thedatadirectory files by running thepython3 organize.pycommand or using the Run button.solutionsdirectory holds the completed code for each of the five files. You can view it at any time if you need further assistance to complete a task.
- Images (
-
Challenge
Create Sub-directories
Your first step is to categorize distinct files into their relevant sub-directories -
images,audios,videos, andscripts. For this step, you will be working in thesrc/starter.pyfile. info> DID YOU KNOW?
You can use the usualforloop to generate the same content instead of using a list comprehension. info> DID YOU KNOW?
If you have to run theos.makedirs()command and are unsure whether the directories already exist on the disk, you can use theexist_ok=Trueargument in theos.makedirs()command. This will leave existing directories unaltered and will not raise any errors or create new directories. info> DID YOU KNOW?
You can create anif/elifstatement without including theelsestatement. Once you have completed the tasks above, you would have created four sub-directories and moved all files to their respective data type directories. Inside file tree, you must see the followingstaticdirectory structure:static
audios
- base.m4a
- base.mp3
- core.m4a
- core.mp3
- evening.m4a
images
- scarlet.gif
- spring.jpg
- sunset.jpg
- vintage.png
scripts
- flour.json
- fruit.json
- vegetable.json
videos
- cheesy.mp4
- soy.mp4
- windy.mp4
- sizzler.mp4
-
Challenge
Sort Image Files According to their Size
The next step is to sort images in the
static/imagesdirectory as per their file size in an ascending order. Given below is a bar plot that shows the available files and their sizes:| Current Layout | Expected Layout | | -------- | -------- | | scarlet.gif | 1_scarlet.gif | | spring.gif | 2_spring.gif | | sunset.gif | 3_vintage.png | | vintage.png | 4_sunset.gif |
For this step, you will work in the
src/images.pyfile. You will start by retrieving the size of each file, sorting them according to their sizes, and finally performing the action within the directory. info> TRY IT OUT!
Rerun this task and print the output of this function. Observe how the list elements do not maintain an order. This is due to theos.listdir()command which follows no order while fetching files. info> DID YOU KNOW?
If you don't want to use thesorted()function, you can always go with thesort()method. It also accepts akeyargument just like thesorted()function. After completing these three steps, you now have arrived at the expected layout inside thestatic/imagesdirectory. -
Challenge
Handle Duplicate Audio File Names
In this step, you will switch to the
src/audios.pyfile and will work within thestatic/audiosdirectory. Here, you have to rename files based on the following format:If a file has a unique name, suffix
_0.If multiple files have similar names, suffix
_0,_1,_2, etc.The duplicate name frequency of each file is depicted in the given figure:

| Current Layout | Expected Layout | | -------- | -------- | | base.m4a | base_0.m4a | | base.mp3 | base_1 .mp3 | | core.m4a | core_0.m4a | | core.mp3 | core_1 .mp3 | | evening.m4a | evening_0.m4a |
The script already has file names and their extensions saved in the variables
aud_filesandaud_exts. info> DID YOU KNOW?
You can usepandasto complete the above task. You can use thepivot_table()function from thepandaspackage to achieve this task as well. You must set the value of theaggfuncargument tosize. After completing these two tasks, you have arrived at the expected layout inside thestatic/audiosdirectory. -
Challenge
Archive Video Files Older than One Year
You have successfully completed half of the lab!
Next, you must archive video files that are older than one year. To do so, you must first extract the last modified time and subtract it from the current time. If the file is older than 365 days, you compress it using the
zipfilepackage.The image depicts the last modified time of each video file:

You will be working in the
src/videos.pyfile and thestatic/videosdirectory. Thevideos.pyfile already has a variablepathsthat holds complete path links to all videos. info> DID YOU KNOW?
A file is associated with three timestamps—access, modify, and create. In this task, you are using the last file modification time. To use other timestamps, you can rely onos.path.getatime()andos.path.getctime()commands. info> DID YOU KNOW
Python offers different modules for compression, employing algorithms such as gzip, bzip2, lzma, and more. Comprehensive information about these algorithms can be found here.info> FOOD FOR THOUGHT
Have you wondered what would happen if you open the file without using thewithstatement? Hint: Try to read the file content outside thewithstatement. After successfully completing the above two tasks, you should be seeing thecomp_vids.zipdirectory inside thestatic/videosdirectory.If you run this lab before November 25, 2024, the zip folder will hold only the
windy.mp4file. However, if you run this lab after the above date, you will end up compressing all the files. -
Challenge
Encrypt and Decrypt Scripts
In the last step of this lab, you will be performing encryption and decryption of multiple files lying inside the
static/scriptsdirectory using thesrc/scripts.pyfile.The script uses the
Fernetmodule of thecryptographypackage to perform this step. It already has a function namedload_key()which loads and returns the key available at/workspace/mykey.key.You will be using this key to encrypt and decrypt all three JSON files.
Before starting with the encryption process, open one of the three files and examine the data to get a sense of its appearance. info> FOOD FOR THOUGHT
1. What would happen if you do not use the valuebwhile reading (rb) and writing (wb) files? Hint: Run the function and check its response.
2. Would it be possible to read files in their alphabetical sorted order before encrypting them? Hint:sorted()function.
3. Can you create your own key instead of using the providedmykey.keytoken? Hint:Fernet.generate_key()function. info> FOOD FOR THOUGHT
1. What would happen if you keep themykey.keyfile inside thestatic/scriptsdirectory before encrypting the directory files? Hint: Are you able to decrypt your files? If not, check the content ofmykey.keyfile. Is the token same as it was before encryption?
2. What would happen if you do not store your key in the disk and initiate theencrypt_files()anddecrypt_files()functions in separate runs? Hint: Check the key in both runs. Is it the same?info> DID YOU KNOW?
You have the flexibility to run theencrypt_files()function multiple times, and each run will generate a distinct set of encrypted data. For decryption and retrieval of the original data, it is crucial to run thedecrypt_files()function an equal number of times as the encryption function. Bravo! You have successfully organized yourstaticdirectory and its distinct data types.Using Python CLI File Organizer
You are now ready to apply these functions on new data using the Terminal. Observe the
datadirectory and theorganize.pyfile in the filetree.Run the
organize.pyfile in the Terminal and follow the menu's steps to organize thedatafiles.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.