python快速提取各个子文件夹下指定格式文件名字,并写入txt文件中
编码环境:python3.8
python3.8
import os
# 获取当前路径
current_path = "C:/Users/10474/Desktop/test"
# 定义输出文件路径
out_path = os.path.join(current_path, 'mp4name.txt')
# 创建输出目录
os.makedirs(os.path.dirname(out_path), exist_ok=True)
# 遍历当前路径下的所有文件夹
for subdir in os.listdir(current_path):
subdir_path = os.path.join(current_path, subdir)
print(subdir_path)
# 判断子文件夹是否存在
if os.path.isdir(subdir_path):
# 遍历子文件夹下的文件
for file in os.listdir(subdir_path):
file_path = os.path.join(subdir_path, file)
# 判断文件是否以 .mp4 结尾,可以换成你的任意格式
if os.path.isfile(file_path) and file.endswith('.mp4'):
# 将文件名写入输出文件
with open(out_path, 'a') as f:
f.write(os.path.splitext(file)[0] + '\n')
f.flush()
f.close()
# 打印路径信息
print(current_path)
print(out_path)
批处理命令:
@echo off
python get_mp4_name.py
pause
python3测试代码文件:
链接:https://pan.baidu.com/s/1oGZJdLCV3RpQetaRDVODDA
提取码:9a89

