|||

Python Scripts for Duplicating Files and Folders

Screenshot of Python script in VSCodeScreenshot of Python script in VSCode

Earlier this summer, I spent some time creating Python scripts to copy files and folders.

Why do that? For me, the most common use case for copying files is that I want to have a marking sheet for each student. The starting point (the blank sheet) is the same for everyone; there aren’t any differences until I actually start marking.

I could, of course, copy the file as many times as I have students, and then rename each file as needed. That works, but it’s very tedious: copy the file, rename the duplicate, copy the file again, rename the new duplicate . . . and keep repeating the process until I run out of students. Doing it manually takes more time than I’d prefer to. In short, it’s a task that would benefit from automation.

The process is easy to put into pseudo-code:

for every student in the class:
    copy the blank marking sheet
    rename the marking sheet with the student's name

All that’s needed is a list of names for the renamed files. Then it’s possible to work through that list, using each name once in the renaming process.

This kind of automated process is something that’s fairly easy to do in Python. With a little trial and error, I was able to put the following script together pretty quickly:

import shutil, os

# Create an array holding the names of the files to be created.
input_names = input("Enter a list of file names, separated by commas. Do not leave spaces between the names. Names: ")
file_names = input_names.split(",")
# print(file_names)

# Get the details of the file to be copied.
src_dir = input("Enter the full path of the directory where the file to be copied is found, including both the beginning / and the trailing /: ")
src_file = input("Enter the name of the file, without the extension: ")
src_type = input("Enter the extention for the file type, including the dot — for example, .pdf: ")
src = src_dir + src_file + src_type

# Get the destination directory.
dest_dir = input("Enter the full path of the directory where the copied files will be stored, including both the beginning / and the trailing /: ")
dest = dest_dir + src_file + src_type

# Set a counter variable.
file_num = 0

# Start a loop that iterates through the names, creating a new file for each and placing it in the new directory.
while file_num < len(file_names):
    # Copy the file to the destination directory.
    shutil.copyfile(src, dest)
    # Rename the file.
    new_name = file_names[file_num]
    new_dest = dest_dir + new_name + src_type
    os.rename(dest, new_dest)
    file_num += 1

The script works well, and it’s very fast. It can easily be hard-coded with a list of names so that it isn’t necessary to ask for that information every time. For those who don’t have Python installed, the script can be run in Google Colab (after connecting Google Drive).

Sometimes the task is to copy a folder instead of a file. For example, I might ask students to put together a portfolio of their work, and I want them all to use the same structure. In that case, I’d want to provide all of them with that structure, in the form of a folder structured something like this:

My portfolio folder
    — My best work
    — Other work

The shutil module’s copytree operation will recursively copy a directory (folder) and its contents, so that’s what’s needed here. The basic logic is essentially the same as for copying a single file:

for every student in the class:
    copy the folder and everything in it
    rename the top-level folder with the student's name

Here’s a Python script that will do that:

# Code from https://www.geeksforgeeks.org/copy-a-directory-recursively-using-python-with-examples/, accessed 12 June 2022. Modified (added loop) for my own use case.
        
# importing shutil module 
import shutil

# Create an array holding the names of the folders to be created.
input_names = input("Enter a list of folder names, separated by commas. Do not leave spaces between the names. Names: ")
folder_names = input_names.split(",")

# Set a counter variable.
folder_num = 0
    
# Source path 
src = input("Enter the full path of the folder you want to copy, for example: /Users/username/Desktop/TestFolder: ")
# src = "/Users/acavender/Desktop/TestFolder"

# Destination path 
base_dest = input("Enter the full path where the copied folders should be placed. Be sure to include a / at the end of the path: ")

# Start a loop that iterates through the names, creating a new folder for each.
while folder_num < len(folder_names):
    dest_plus = folder_names[folder_num]
    dest = base_dest + dest_plus
    # Copy the content of source to destination
    destination = shutil.copytree(src, dest)
    folder_num += 1

As with the script for copying single files, it’s easy to hard-code names into this script, and to run in Google Colab instead of from the command line.

Up next A Shortcut for Randomly Calling on Students Duplicating Files and Folders With Shortcuts
Latest posts A Shortcut for Creating Random Groups Data Design and Shortcuts Duplicating Files and Folders With Shortcuts Python Scripts for Duplicating Files and Folders A Shortcut for Randomly Calling on Students The Bento Method With Things and Obisidian Importing a WordPress Xml File WordPress Export Some Thoughts on Austin Kleon’s Show Your Work! Adding Google Fonts to a Ghost Theme A Course Planning Template An Unexpected Use for a Raspberry Pi One Year Ago… Sabbatical Diary: Thinking About Grades Paperless Handwritten Feedback Filenames and LaTeX and Pandoc, Oh My! Collecting Student Work With Google Forms