格式字幕格式脚本

import os

import re

获取Python脚本所在的目录

directory = os.path.dirname(os.path.realpath(file))

遍历目录及其所有子目录下的所有文件

for root, dirs, files in os.walk(directory):

for filename in files:

如果文件是.srt文件

if filename.endswith('.srt'):

path = os.path.join(root, filename)

with open(path, 'r', encoding='utf-8') as file:

lines = file.readlines()

移除空白行

new_lines = line for line in lines if not re.match(r'\^\\s\*$', line)

将处理过的行写回文件

with open(path, 'w', encoding='utf-8') as file:

file.writelines(new_lines)


import os

import re

def insert_blank_line(filename):

with open(filename, 'r', encoding='utf-8') as file:

lines = file.readlines()

new_lines = \[\]

for line in lines:

if re.match(r'^1-9\d*', line.strip()):

new_lines.append('\n')

new_lines.append(line)

with open(filename, 'w', encoding='utf-8') as file:

file.writelines(new_lines)

def find_srt_files(path):

for root, dirs, files in os.walk(path):

for file in files:

if file.endswith('.srt'):

insert_blank_line(os.path.join(root, file))

if name == "main":

find_srt_files('.')

相关推荐
疯狂学习GIS4 天前
基于Python earthaccess库批量下载全球MODIS GPP(MOD17A2HGF)数据
python·脚本·批量下载·遥感影像·nasa·earthdata·自动处理
大大杰哥15 天前
Windows 批处理语法笔记:从入门到一键部署项目
脚本·命令行
一念&16 天前
油猴脚本教程——元数据块
javascript·浏览器·脚本·油猴
console.log('npc')19 天前
Windows试用期重置工具,纯官方 / 安全教程
运维·自动化·typora·脚本
Hehuyi_In1 个月前
postgres-howto 学习笔记
笔记·学习·postgresql·脚本·how to
Android出海1 个月前
2026年Codex新手教程:安装、使用与自动化实战指南
人工智能·ai·chatgpt·自动化·脚本·codex·自动化脚本
TA远方1 个月前
【JavaScript】Promise对象使用方式研究和理解
javascript·编程·脚本·web·js·promise·委托
代码中介商2 个月前
Makefile工程管理完全指南:从基础到实践
linux·脚本
七夜zippoe2 个月前
DolphinDB脚本语言:从入门到精通
后端·struts·脚本·语言·dolphindb
Yiyi_Coding2 个月前
bat 脚本(真实项目可用):ftp取远程文件
运维·脚本·ftp