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
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:
<span>import</span> <span>os</span><span>current_directory</span> <span>=</span> <span>os</span><span>.</span><span>getcwd</span><span>()</span> <span># => this is a string for our current directory </span><span>import</span> <span>pathlib</span><span>cwd_as_obj</span> <span>=</span> <span>pathlib</span><span>.</span><span>Path</span><span>(</span><span>current_directory</span><span>)</span> <span># => turn that string into a path object </span><span>items</span> <span>=</span> <span>list</span><span>(</span><span>cwd_as_obj</span><span>.</span><span>iterdir</span><span>())</span> <span># => list the paths inside our cwd </span><span>folder_count</span> <span>=</span> <span>0</span><span>existing_count</span> <span>=</span> <span>0</span><span>new_count</span> <span>=</span> <span>0</span><span>for</span> <span>item</span> <span>in</span> <span>items</span><span>:</span><span>if</span> <span>item</span><span>.</span><span>is_dir</span><span>():</span> <span># if this is a folder </span> <span>folder_count</span> <span>=</span> <span>folder_count</span> <span>+</span> <span>1</span> <span># count it </span> <span>file_to_create</span> <span>=</span> <span>f</span><span>"</span><span>{</span><span>item</span><span>}</span><span>/</span><span>{</span><span>item</span><span>.</span><span>name</span><span>}</span><span>_Notes.txt"</span> <span># name a notes file to make (string) </span><span>if</span> <span>os</span><span>.</span><span>path</span><span>.</span><span>exists</span><span>(</span><span>file_to_create</span><span>):</span> <span># if a notes file already exists </span> <span>print</span><span>(</span><span>"File Exists"</span><span>)</span><span>existing_count</span> <span>=</span> <span>existing_count</span> <span>+</span> <span>1</span> <span># count it </span> <span>else</span><span>:</span> <span># otherwise </span> <span>print</span><span>(</span><span>f</span><span>"Creating File </span><span>{</span><span>folder_count</span><span>}</span><span>: </span><span>{</span><span>file_to_create</span><span>}</span><span>"</span><span>)</span><span>new_count</span> <span>=</span> <span>new_count</span> <span>+</span> <span>1</span> <span># count it </span> <span>with</span> <span>open</span><span>(</span><span>file_to_create</span><span>,</span> <span>"x"</span><span>)</span> <span>as</span> <span>f</span><span>:</span> <span># make a new notes file </span> <span>f</span><span>.</span><span>write</span><span>(</span><span>f</span><span>"</span><span>{</span><span>item</span><span>.</span><span>name</span><span>}</span><span> Notes:"</span><span>)</span> <span># populate it with a correct heading </span> <span>f</span><span>.</span><span>close</span><span>()</span><span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>folder_count</span><span>}</span><span> Folders found in this directory."</span><span>)</span><span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>existing_count</span><span>}</span><span> Notes Files found in subdirectories."</span><span>)</span><span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>new_count</span><span>}</span><span> new Notes Files created."</span><span>)</span><span>import</span> <span>os</span> <span>current_directory</span> <span>=</span> <span>os</span><span>.</span><span>getcwd</span><span>()</span> <span># => this is a string for our current directory </span> <span>import</span> <span>pathlib</span> <span>cwd_as_obj</span> <span>=</span> <span>pathlib</span><span>.</span><span>Path</span><span>(</span><span>current_directory</span><span>)</span> <span># => turn that string into a path object </span> <span>items</span> <span>=</span> <span>list</span><span>(</span><span>cwd_as_obj</span><span>.</span><span>iterdir</span><span>())</span> <span># => list the paths inside our cwd </span> <span>folder_count</span> <span>=</span> <span>0</span> <span>existing_count</span> <span>=</span> <span>0</span> <span>new_count</span> <span>=</span> <span>0</span> <span>for</span> <span>item</span> <span>in</span> <span>items</span><span>:</span> <span>if</span> <span>item</span><span>.</span><span>is_dir</span><span>():</span> <span># if this is a folder </span> <span>folder_count</span> <span>=</span> <span>folder_count</span> <span>+</span> <span>1</span> <span># count it </span> <span>file_to_create</span> <span>=</span> <span>f</span><span>"</span><span>{</span><span>item</span><span>}</span><span>/</span><span>{</span><span>item</span><span>.</span><span>name</span><span>}</span><span>_Notes.txt"</span> <span># name a notes file to make (string) </span> <span>if</span> <span>os</span><span>.</span><span>path</span><span>.</span><span>exists</span><span>(</span><span>file_to_create</span><span>):</span> <span># if a notes file already exists </span> <span>print</span><span>(</span><span>"File Exists"</span><span>)</span> <span>existing_count</span> <span>=</span> <span>existing_count</span> <span>+</span> <span>1</span> <span># count it </span> <span>else</span><span>:</span> <span># otherwise </span> <span>print</span><span>(</span><span>f</span><span>"Creating File </span><span>{</span><span>folder_count</span><span>}</span><span>: </span><span>{</span><span>file_to_create</span><span>}</span><span>"</span><span>)</span> <span>new_count</span> <span>=</span> <span>new_count</span> <span>+</span> <span>1</span> <span># count it </span> <span>with</span> <span>open</span><span>(</span><span>file_to_create</span><span>,</span> <span>"x"</span><span>)</span> <span>as</span> <span>f</span><span>:</span> <span># make a new notes file </span> <span>f</span><span>.</span><span>write</span><span>(</span><span>f</span><span>"</span><span>{</span><span>item</span><span>.</span><span>name</span><span>}</span><span> Notes:"</span><span>)</span> <span># populate it with a correct heading </span> <span>f</span><span>.</span><span>close</span><span>()</span> <span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>folder_count</span><span>}</span><span> Folders found in this directory."</span><span>)</span> <span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>existing_count</span><span>}</span><span> Notes Files found in subdirectories."</span><span>)</span> <span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>new_count</span><span>}</span><span> new Notes Files created."</span><span>)</span>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:
<span>wordOne</span> <span>=</span> <span>"</span><span>Hello</span><span>"</span><span>wordTwo</span> <span>=</span> <span>"</span><span>World</span><span>"</span><span>console</span><span>.</span><span>log</span><span>(</span><span>`</span><span>${</span><span>wordOne</span><span>}</span><span> </span><span>${</span><span>wordTwo</span><span>}</span><span>!`</span><span>)</span> <span>// => "Hello World!"</span><span>wordOne</span> <span>=</span> <span>"</span><span>Hello</span><span>"</span> <span>wordTwo</span> <span>=</span> <span>"</span><span>World</span><span>"</span> <span>console</span><span>.</span><span>log</span><span>(</span><span>`</span><span>${</span><span>wordOne</span><span>}</span><span> </span><span>${</span><span>wordTwo</span><span>}</span><span>!`</span><span>)</span> <span>// => "Hello World!"</span>wordOne = "Hello" wordTwo = "World" console.log(`${wordOne} ${wordTwo}!`) // => "Hello World!"
Enter fullscreen mode Exit fullscreen mode
vs py:
<span>word_one</span> <span>=</span> <span>"Hello"</span><span>word_two</span> <span>=</span> <span>"World"</span><span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>word_one</span><span>}</span><span> </span><span>{</span><span>word_two</span><span>}</span><span>!"</span><span>)</span> <span># => "Hello World!" </span><span>word_one</span> <span>=</span> <span>"Hello"</span> <span>word_two</span> <span>=</span> <span>"World"</span> <span>print</span><span>(</span><span>f</span><span>"</span><span>{</span><span>word_one</span><span>}</span><span> </span><span>{</span><span>word_two</span><span>}</span><span>!"</span><span>)</span> <span># => "Hello World!" </span>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
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
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
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
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
You can check it out and try the script for yourself on my GitHub.
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
暂无评论内容