文件夹里面有几百个mp3文件,现在要讲所有文件标题名提取到一个txt文档中。 在kimi中输入提示词: 你是一个Python编程专家,一步一步的思考,完成以下Python脚本撰写任务: 打开文件夹:E:\All-In Podcast 读取里面所有的mp3文件,按照文件名进行排序; 按照排序好的文件名提取所有mp3标题名(不含扩展名)到txt文档中; txt文档保存到文件夹:E:\All-In Podcast 注意:每一步都要输出信息到屏幕上 Kimi第一次生成的代码,运行后,有些问题: 让kimi修改一下: ###E108_ Doxing debate, Nuclear fusion breakthrough, state of the markets & more E109_ 2022 Bestie Awards Live from Twitter HQ E10_ Twitter & Facebook botch censorship (again), the publisher vs. distributor debate & more E110_ 2023 Bestie Predictions! E111_ Microsoft to invest $10B in OpenAI, generative AI hype, America's over-classification problem E112_ Is Davos a grift_ Plus_ globalist mishaps, debt ceilings, TikTok's endgame & more E113_ DOJ tries to break up Google, vaccine questions, Ukraine escalation & more E114_ Markets update_ whipsaw macro picture, big tech, startup mass extinction event, VC reckoning ### 部分文件名是这样排序的,我希望达到的效果是E10在E9之前,按照E后面数字大小排序,而不是E109、E110之间 Kimi的回复: 要实现按照文件名前缀(E后面数字)的自然排序,我们可以使用Python的natsort库,它支持自然排序,即能够识别数字并按照自然顺序进行排序,而不是按照字符顺序。 首先,你需要安装natsort库,如果还没有安装,可以通过以下命令安装: pip install natsort 然后,你可以使用以下脚本来完成你的任务: import os import re from natsort import natsorted # 定义文件夹路径 folder_path = 'E:\\All-In Podcast' output_file_path = os.path.join(folder_path, 'podcast_titles.txt') # 打开文件夹并读取所有mp3文件 files = os.listdir(folder_path) # 筛选出mp3文件并按文件名前缀自然排序 mp3_files = natsorted([f for f in files if f.endswith('.mp3')]) print(f"找到了 {len(mp3_files)} 个mp3文件,排序完成。") # 打开输出的txt文档准备写入 with open(output_file_path, 'w', encoding='utf-8') as output_file: for mp3_file in mp3_files: # 使用正则表达式提取标题名(假设标题名不包含点) title_match = re.match(r'^(E\d+_.*)\.mp3$', mp3_file) if title_match: title = title_match.group(1) print(f"提取标题:{title}") output_file.write(title + '\n') else: print(f"无法从文件名 {mp3_file} 中提取标题。") print(f"所有标题已成功提取到 {output_file_path}") 这次排序就达到预期效果了。 |
|