Thanks to my birthday gift i created my first python script

A couple of weeks back was my birthday and although i don’t really celebrate, my daughter wanted to get me a gift. Because we’ve gotten a new second hand car with an old tape deck that doesn’t support aux or something, i saw a so-called FM Transmitter. This is a device you place in you cigarette lighter in the car and you can connect a phone (over Bluetooth or AUX cable) or a simple USB stick. The AUX connection is crap! A lot of noise, but the BT and USB connection are oké. Main thing that i found as a disadvantage is that when you play music over the USB stick the music isn’t random. If you don’t like an album, you have to skip through al the songs of the album and hope you get a nice record after going through.

To solve this (and to learn the Python scripting language) i’ve created the next script:

import os
from shutil import copyfile
from random import randint
import pathlib

MP3_FOLDER=r'Source  folder'
DEST_FOLDER = 'Destination Folder'
FileCount = 1
for root,dirs,files in os.walk(MP3_FOLDER):
    for file in files:
        if file.endswith('.mp3'):
            print(FileCount)
            FileCount = FileCount + 1
            rndFolder = DEST_FOLDER+str(randint(100, 999))
            rndFile = randint(10000, 99999)
            pathlib.Path(rndFolder).mkdir(parents=True, exist_ok=True)
            srcfile = os.path.join(root,file)
            dstfile = rndFolder+'\\'+str(rndFile)+'.mp3'
            copyfile(srcfile,dstfile)</pre>

It scans the source folder and for every file that ends with ‘.mp3’ it generates a random number between 100 and 999, and creates a folder of it. Then it generates a random number between 10000 and 99999 and makes a file out of it with the extension ‘.mp3’. In my situation there are 5000+ mp3 files, so there will be a descent amount of directories, with different filenames in them. When the files are done, i can copy them from the destination folder to my USB stick. Of course the destination folder can already be the USB stick.

If you are interested in the FM Transmitter, search for ‘BC06’. This is a very generic FM transmitter. I think you can buy them in China and sell them under your own brand. I’ve seen them from MFEEL, Kebidumei, Kebidu etc etc etc.