0

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

Alucard
  • 1
  • 1
  • Your `dirs` variable also contains the filenames, so you are trying to list the files under `path\\to\\file\\makem3u.py` (obviously this is not a directory). Use `os.walk` instead: https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory or use `os.path.isdir()` to check if `d` is a directory. – Selcuk Sep 09 '20 at 05:44
  • Thanks for your reply. I get the idea, so if the input is a directory then the script will work but not if the input is a file. The problem is I have little to no knowledge on how to do this. I tried to replace the commands but I'm getting other errors... I read about it, but I didn't quite understand – Alucard Sep 09 '20 at 06:16
  • Just add a `if os.path.isdir(d):` right after your `for` line for now. – Selcuk Sep 09 '20 at 06:52
  • 1
    Thanks, that worked. I was getting another error `TypeError: a bytes-like object is required, not 'str'` , but after I replaced `'wb'` for `'w'` it's now generating the m3u files. Now I'm trying to figure out why it is writing "Folder name\File names" instead of just the "File names" in the m3u. Also the m3u files all appear in the parent folder instead of each subfolder its named after, but that's a minor thing I can get around. – Alucard Sep 10 '20 at 19:08

0 Answers0