Well it’s Christmas Eve, and I had some free time so I wanted to try out a mini programming project that would be useful. It’s been annoying me (very OCD…) for ages that my MagPi (Raspberry Pi magazine) downloads are not in order – after the Raspberry Pi foundation took over, it changed the filenaming system, from “The-MagPi-issue-xx-en.pdf” to “MagPiXX.pdf”. It’s too much effort to rename 50 odd files by hand – so why not use python?

My code is below. Some useful things I learnt;
1. String cutting using square brackets;
2. I used python on Bash on Ubuntu on Windows, so I had to access the windows filesystem at /mnt/c/

import os
home_dir="/mnt/c/Users/Andrew/Dropbox/Tech Guides/MagPi"
for filename in os.listdir(home_dir):
    if filename.endswith("-en.pdf"):
        filename = home_dir + "/" + filename
        newname= home_dir + "/MagPi" + filename[-9:-7] + ".pdf"
        os.rename(filename, newname)