Python script to create m3u files inside folders

1.4k Views Asked by At

I'm trying to run this specific python script to create .m3u files inside subfolders but it's not working and give me an error, any help is welcome. The m3u file is a simple tracklist of the subfolder content with specified extensions and named after the subfolder, like this (extensions .aaa and .bbb are just examples):

  • Folder 1 (contains 'File 1.aaa', 'File 2.aaa', etc)

Folder 1.m3u generated inside Folder 1 with this list

File 1.aaa

File 2.aaa

  • Folder 2 (contains 'File 1.bbb', 'File 2.bbb', etc)

Folder 2.m3u generated inside Folder 2 with this list

File 1.bbb

File 2.bbb

Here is the script called makem3u.py (not mine, I don't know much about python):

#!/usr/bin/python
"""This script will create an m3u file with a tracklist of each .aaa or .bbb 
found in a subdirectory, then name the m3u after the subdirectory. This helps 
with multiple disks for emulation.
"""

import os
import os.path

EXT = ['.aaa', '.bbb']

cwd = os.getcwd()
dirs = os.listdir(cwd)

for d in dirs:
    contents = os.listdir(os.path.join(cwd, d))
    disks = [
        os.path.join(d, f) for f in os.listdir(os.path.join(cwd, d)) 
        if os.path.splitext(f)[-1] in EXT
    ]
    if disks:
        with open(os.path.join(cwd, '{d}.m3u'.format(d=d)), 'wb') as m3u:
            m3u.writelines(['{disk}\n'.format(disk=disk) for disk in disks])

I get this error when I try to run it:

Traceback (most recent call last):
  File "makem3u.py", line 16, in <module>
    contents = os.listdir(os.path.join(cwd, d))
NotADirectoryError: [WinError 267] The directory name is invalid: 'path\\to\\file\\makem3u.py'

makem3u.py is inside a folder with the subfolders mentioned

Windows 10, Python 3.8.5, python is installed properly and the PATH is in enviroment variables and I can run other scripts just fine

What I'm doing wrong and how can I fix this? Is there a non-python alternative like create a .bat file to do that? Sorry for so many questions, I'm a noob in these things. Thank you in advance!

Also, is there a way to batch zip all the files in the subfolders (the generated m3u + original files) and name each zip after that subfolder? This is an extra, but would be helpful if possible

0

There are 0 best solutions below