This is just another simple post on how I write Python scripts to help me with manually taxing tasks. I included a script in this post that made it easier for me to rename files in bulk. If you are curious on how I automate some boring tasks, read along: python_script_1

My Birthday Gift/s: E-books

Every year, I treat myself by purchasing a product that I would not buy unless I slept on it and still thought that I needed it, waited enough time for it to go on “sale”, or if it would give me an immediate reward for a particular day like today, 04/13.

I am not a purist or someone who claims to love the smell of books, their pages, the ink …. and the feeling of turning actual pages. Some part of me might be that way, but I am someone who does not mind owning the exact book and its electronic version:

Picture of books I own

So, for my birthday this year, I finally subscribe to a month worth of downloads (mostly Ebooks) from Scribd.com https://www.scribd.com:

Scribd.com

Since I enjoy learning and a good book that entertains me, I decided to download as much ebooks and added them to my “collections”. I find it amusing that the app Readera categorizes their books as “collections” because I collect them until I find time to read most of them. “I will read them eventually “ is my answer to anyone who asks me why I bother buying (or legally downloading) these books.

What do I love about E-books? I love the portability of e-books and the convenience of using an e-book reader with text-to-speech features. I have Amazon Kindle (with Audible integration) https://www.amazon.com/kindle-dbs/storefront and (Readera) https://readera.org/:

Readera App

The Problem (Not really a big one but a personal one)

My only issue, which presents a challenge, is that Scribd.com downloaded files come with something extra. They are prefixed with some gibberish code, and initially, I had to rename them one by one.

python_script_1

Since I only plan to keep the subscription for a month (pending a decision to extend), I downloaded as many books as I could, and renaming them one by one became a task I didn’t want to do repeatedly. I had to automate:

The Solution: Scripting

I wanted to use Bash scripting, but I already had done that multiple times: E.g. https://github.com/roylouisgarcia/fsrt

So, I decided to write a Python script to do it.

Below is how I did it. Use it if you find it useful:

import os

path = "./downloaded_files"
dir_list = os.listdir(path)

for i in dir_list:
    print(i)
    if i.__contains__('-'):
        b = i.split('-', 1)[1]
        old_file = os.path.join(path, i)
        new_file = os.path.join(path, b)
        os.rename(old_file, new_file)
    else:
        print ("do nothing")

Here, I only use the first “hyphen” as a cutoff point to what I wanted to take out from the file name and what to keep:

Picture of my code and the pseudocode

Before (note the filenames with the gibberish prefix):

Before Running the Script

Note: I inserted some “print to console” statements just to see what is happening, but I eventually took them out.

Verbose screenshot of running the script

After (see the cleaner file names resulting from the script that ran):

Results of running the script

Conclusion

Establishing a way to keep track of things, such as maintaining an inventory of our books, can be a challenge. However, a well-structured naming system can significantly simplify this task. With Python scripting, we are in full control of how we want the bulk files, created by another application, to be renamed. This level of control instills confidence and ensures that the renaming process aligns perfectly with our requirements. Control over things is important on any other day, especially my special day. Happy Birthday to me. <3