DEV Community

Cover image for Python Automation - 01
Elliot Mangini
Elliot Mangini

Posted on

Python Automation - 01

Python Program to Generate Notes File for Each Subdirectory

If you prefer: GitHub with script download.

I recently watched a one-hour Python class offered by StudySession to learn the basics of Python syntax. The class covered topics such as variables, data types, conditionals, loops, functions, classes and all the basics I would need to get started because I wanted to try writing my first automation stuff. The following is...
 

My First Automation Script!

The problem I'm solving is that I have 600+ songs and I want to make a place to take notes on them as I work on them, and I wanna do it quickly.
 

What I want to end up with:

Notes Files for All Songs

Notes Files for All Songs

 

I have another simple automation project I'll post about soon, and then more as I've found when you are managing a project with a discography of several hundred songs it is difficult to do even the simplest things without automation!!

 

The actual script:

import os
current_directory = os.getcwd()  # => this is a string for our current directory

import pathlib
cwd_as_obj = pathlib.Path(current_directory)  # => turn that string into a path object

items = list(cwd_as_obj.iterdir())  # => list the paths inside our cwd

folder_count = 0
existing_count = 0
new_count = 0

for item in items:
    if item.is_dir():  # if this is a folder
        folder_count = folder_count + 1  # count it
        file_to_create = f"{item}/{item.name}_Notes.txt"  # name a notes file to make (string)

        if os.path.exists(file_to_create):  # if a notes file already exists
            print("File Exists")
            existing_count = existing_count + 1  # count it
        else:  # otherwise
            print(f"Creating File {folder_count}: {file_to_create}")
            new_count = new_count + 1  # count it
            with open(file_to_create, "x") as f:  # make a new notes file
                f.write(f"{item.name} Notes:")  # populate it with a correct heading
                f.close()

print(f"{folder_count} Folders found in this directory.")
print(f"{existing_count} Notes Files found in subdirectories.")
print(f"{new_count} new Notes Files created.")
Enter fullscreen mode Exit fullscreen mode

It starts by importing the os module to retrieve the current working directory path as a string. Then it uses the pathlib module to convert the string path to a path object. After that, it lists all the paths inside the current working directory using the .iterdir() method.

The program then initializes three variables: folder_count, existing_count, and new_count, all with an initial value of 0. The program then enters a for loop that iterates over each item (i.e., file or directory) inside the current working directory. If the item is a directory (i.e., a subdirectory), the program increments the folder_count variable and proceeds to create a notes file for that directory.

Quick aside-- if you're coming from JS this is how string interpolation/template literals work in python:

in JS:

wordOne = "Hello"
wordTwo = "World"
console.log(`${wordOne} ${wordTwo}!`)  // => "Hello World!"
Enter fullscreen mode Exit fullscreen mode

vs py:

word_one = "Hello"
word_two = "World"
print(f"{word_one} {word_two}!")  # => "Hello World!"
Enter fullscreen mode Exit fullscreen mode

Backticks become double-quotes and we use curly braces without a $ before them
 

The name of the notes file is set to {item}/{item.name}_Notes.txt. The first {item} refers to the path of the directory, while {item.name} refers to the name of the directory. The .txt extension is added to denote that this is a text file.

Screenshot of Created Files

Screenshot of Created Files

 

If a notes file already exists in the subdirectory, the program increments the existing_count variable and prints the message "File Exists". Otherwise, the program increments the new_count variable, prints the message "Creating File {folder_count}: {file_to_create}" and proceeds to create a new file with the name and location specified in file_to_create.

Notes File Boilerplate Example Screenshot

Notes File Boilerplate Example

 

To create a new file, the program uses the open() function with the x mode. The x mode creates a new file and opens it for writing. It also populates a header for the text file using f.write(f"{item.name} Notes:").

File Setup Before Script Runs

File Setup Before Script Runs

 

To run it we just use python3 <name_of_script_here.py>.

Altogether here's what I have the console doing for me, accounting for all blocks of code being reached:

Running Script & Showing Terminal Output

Running Script & Showing Terminal Output

 

The program prints a summary of the number of subdirectories found, the number of existing notes files, and the number of new notes files created. The message includes the folder_count, existing_count, and new_count variables.

We end with:

All Notes Created In Correct Places

All Notes Created In Correct Places

 

You can check it out and try the script for yourself on my GitHub.

Preview of my second simple python automation project screenshot.

Preview of my second simple python automation project.

 

Next post will be about how to take all of these folders containing multiple bounces of songs (as they have progressed) ie version 1, version 2, etc. And creating a folder where every song is represented by one file-- whichever is most recent. Smash that follow button to see how I did it. And let me know if there's any way I can make my blogs easier to follow or more useful for those finding them!

Thanks,
-Elliot/Big Sis

Top comments (0)