首页 > 解决方案 > 在文件夹中创建 m3u 文件的 Python 脚本

问题描述

我正在尝试运行此特定的 python 脚本以在子文件夹中创建 .m3u 文件,但它不起作用并给我一个错误,欢迎提供任何帮助。m3u 文件是具有指定扩展名并以子文件夹命名的子文件夹内容的简单跟踪列表,如下所示(扩展名 .aaa 和 .bbb 只是示例):

文件夹 1.m3u使用此列表在文件夹 1 内生成

文件 1.aaa

文件 2.aaa

文件夹 2.m3u使用此列表在文件夹 2 中生成

文件 1.bbb

文件 2.bbb

这是名为 makem3u.py 的脚本(不是我的,我对 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])

当我尝试运行它时出现此错误:

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 位于一个文件夹中,其中提到了子文件夹

Windows 10,Python 3.8.5,python 安装正确,PATH 在环境变量中,我可以很好地运行其他脚本

我做错了什么,我该如何解决?是否有非 python 替代方法,例如创建一个 .bat 文件来做到这一点?抱歉问了这么多问题,我是这些东西的菜鸟。先感谢您!

另外,有没有办法批量压缩子文件夹中的所有文件(生成的 m3u + 原始文件)并在该子文件夹之后命名每个 zip?这是一个额外的,但如果可能的话会有所帮助

标签: pythonbatch-filem3u

解决方案


推荐阅读