{"id":173,"date":"2018-05-02T22:41:29","date_gmt":"2018-05-02T22:41:29","guid":{"rendered":"http:\/\/www.project34.net\/?p=173"},"modified":"2023-10-26T21:20:06","modified_gmt":"2023-10-26T20:20:06","slug":"thanks-to-my-birthday-gift-i-created-my-first-python-script","status":"publish","type":"post","link":"https:\/\/www.project34.net\/index.php\/2018\/05\/02\/thanks-to-my-birthday-gift-i-created-my-first-python-script\/","title":{"rendered":"Thanks to my birthday gift i created my first python script"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A couple of weeks back was my birthday and although i don&#8217;t really celebrate, my daughter wanted to get me a gift. Because we&#8217;ve gotten a new second hand car with an old tape deck that doesn&#8217;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\u00e9. Main thing that i found as a disadvantage is that when you play music over the USB stick the music isn&#8217;t random. If you don&#8217;t like an album, you have to skip through al the songs of the album and hope you get a nice record&nbsp;after going through.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To solve this (and to learn the Python scripting language) i&#8217;ve created the next script:<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">import os\nfrom shutil import copyfile\nfrom random import randint\nimport pathlib\n\nMP3_FOLDER=r'Source  folder'\nDEST_FOLDER = 'Destination Folder'\nFileCount = 1\nfor root,dirs,files in os.walk(MP3_FOLDER):\n    for file in files:\n        if file.endswith('.mp3'):\n            print(FileCount)\n            FileCount = FileCount + 1\n            rndFolder = DEST_FOLDER+str(randint(100, 999))\n            rndFile = randint(10000, 99999)\n            pathlib.Path(rndFolder).mkdir(parents=True, exist_ok=True)\n            srcfile = os.path.join(root,file)\n            dstfile = rndFolder+'\\\\'+str(rndFile)+'.mp3'\n            copyfile(srcfile,dstfile)&lt;\/pre><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It scans the source folder and for every file that ends with &#8216;.mp3&#8217; it generates&nbsp;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 &#8216;.mp3&#8217;. 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are interested in the FM Transmitter, search for &#8216;BC06&#8217;. This is a very generic FM transmitter. I think you can buy them in China and sell them under your own brand. I&#8217;ve seen them from MFEEL,\u00a0Kebidumei,\u00a0Kebidu etc etc etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple of weeks back was my birthday and although i don&#8217;t really celebrate, my daughter wanted to get me a gift. Because we&#8217;ve gotten a new second hand car with an old tape deck&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":2,"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":336,"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions\/336"}],"wp:attachment":[{"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.project34.net\/index.php\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}