Welcome to the second project in this series. In this project, you will modify the code by breaking it into simple functions and refactoring it. This will teach you how to organize your code. By the end of this project, you will be prepared to tackle the remaining exercises.
To access the project file and README, click here.
Writing Code
Open the project README. You are working on Project 1 which is “ Selective Copy”. Make sure you understand what you are to do before moving on to read the code.
This project will teach you how to read people code and improve on the code. In the real-world you will probably be working on code written by previous engineers, you are likely not going to write new codes.
import os, shutil, sys
# walk through a dir tree
# create a list of file extensions to copy
# copy it to a new folder
def select_copy(src, dest):
src = os.path.abspath(src)
dest = os.path.abspath(dest)
extensions = ['.pdf', '.jpg', '.png']
# Check if the provided path is a directory
if not os.path.isdir(src):
print(f'The path "{src}" you have provided is not a directory')
return # used to exit the function
# walk the dir tree
for folder_name, subfolders, filenames in os.walk(src):
for filename in filenames:
for extension in extensions:
if filename.endswith(extension):
print(filename)
# check if the dest exists
if not os.path.exists(dest):
os.mkdir(dest)
# copy it to a new folder
shutil.copy(os.path.join(folder_name, filename), dest)
if __name__ == '__main__':
src = r"C:\Users\Praise Idowu\Desktop\test2"
dest = r"C:\Users\Praise Idowu\Desktop\test4"
select_copy(src, dest)
Run the program. Let us move on to the refactoring phase.
Refactoring
If a variable like subfolders
is not utilized, it is a Python convention to replace it with an underscore (‘_’). Replace subfolders
with an underscore.
for folder_name, subfolders, filenames in os.walk(src):
Create three functions, each with a parameter.
Multiline strings explain the purpose of each function. Implement and test your code.
import os, shutil, sys
def select_files(src, extensions):
"""
Select files with specified extensions from the source directory.
Parameters:
- src (str): Source directory path.
- extensions (list): List of file extensions to select.
Returns:
- list: List of selected file paths.
"""
pass
def copy_files(selected_files, dest):
"""
Copy selected files to the destination directory.
Parameters:
- selected_files (list): List of file paths to copy.
- dest (str): Destination directory path.
"""
pass
def select_copy(src, dest, extensions):
"""
Select files with specified extensions from the source directory
and copy them to the destination directory.
Parameters:
- src (str): Source directory path.
- dest (str): Destination directory path.
- extensions (list): List of file extensions to select.
"""
pass
if __name__ == '__main__':
pass
Exercise
Complete the remaining two exercises in the practice project and share your code as well.
We have come to the end of this series.
Conclusion
You already have the skills needed to organize and manipulate files. Moving forward, you will work on complex automation projects. Until next time, happy coding!
Top comments (0)