There is an immense amount to be learned simply by tinkering with things. - Henry Ford
It's time for another code challenge! This week we're asking you to work with directory, files and audio meta data!
Write a script that receives a directory name and retrieves all mp3 (or mp4 or m4a) files. It then sums up the durations of each file and prints them in a nice table with a total duration.
This could look like the following:
$ module_duration.py ~/Music/iTunes/iTunes\ Media/Music/Manu\ Chao/Manu\ Chao\ -\ Esperanza/ Manu Chao - Bixo.m4a : 112 Manu Chao - Denia.m4a : 279 Manu Chao - El Dorrado 1997.m4a : 89 Manu Chao - Homens.m4a : 198 Manu Chao - Infinita Tristeza.m4a : 236 Manu Chao - La Chinita.m4a : 93 Manu Chao - La Marea.m4a : 136 Manu Chao - La Primavera.m4a : 112 Manu Chao - La Vacaloca.m4a : 143 Manu Chao - Le Rendez Vous.m4a : 116 Manu Chao - Me Gustas Tu.m4a : 240 Manu Chao - Merry Blues.m4a : 216 Manu Chao - Mi Vida.m4a : 152 Manu Chao - Mr Bobby.m4a : 229 Manu Chao - Papito.m4a : 171 Manu Chao - Promiscuity.m4a : 96 Manu Chao - Trapped by Love.m4a : 114 -------------------------------------------------- Total : 0:45:32
Why do we think this is cool? There are a couple of subtasks here:
You learn how to do a common sysadmin task of listing files in a directory (check out the
You learn how to convert and calculate mm:ss (minutes/seconds) timings, which will hone your
subprocessmodule allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. - docs
Good luck and have fun!
A few more things before we take off:
Do you want to discuss this challenge and share your Pythonic journey with other passionate Pythonistas? Confirm your email on our platform then request access to our Slack via settings.
>>> from pybites import Bob, Julian Keep Calm and Code in Python!
Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.
"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H
"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B
"This is now my favourite first Python go-to reference." - Anthony L
"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S