格式字幕格式脚本

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('.')

相关推荐
寒水馨5 天前
macOS下载、安装PowerShell-v7.6.4(附安装包powershell-7.6.4-osx-arm64.pkg)
macos·自动化·跨平台·shell·脚本·命令行·powershell
逻极12 天前
Shell脚本实战:从“能跑”到“高效”的3个关键跃升
linux·服务器·shell·脚本
至乐活着21 天前
Shell脚本编写最佳实践:打造健壮、可维护的自动化利器
自动化·bash·shell·脚本·最佳实践
xcLeigh1 个月前
KES运维自动化与脚本体系实战
运维·数据库·自动化·脚本·数据迁移·kes
CAU界编程小白1 个月前
CAU抢课脚本
c++·脚本
疯狂学习GIS2 个月前
基于Python earthaccess库批量下载全球MODIS GPP(MOD17A2HGF)数据
python·脚本·批量下载·遥感影像·nasa·earthdata·自动处理
大大杰哥2 个月前
Windows 批处理语法笔记:从入门到一键部署项目
脚本·命令行
一念&2 个月前
油猴脚本教程——元数据块
javascript·浏览器·脚本·油猴
console.log('npc')2 个月前
Windows试用期重置工具,纯官方 / 安全教程
运维·自动化·typora·脚本
Hehuyi_In3 个月前
postgres-howto 学习笔记
笔记·学习·postgresql·脚本·how to