格式字幕格式脚本

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

相关推荐
Freshman小白1 个月前
Fluent自动化仿真(TUI命令脚本教程)
自动化·脚本·仿真
水哥ansys2 个月前
ANSYS ACT 格式及文件组成如何?
二次开发·脚本·ansys·workbench·水哥ansys·act
ZZZKKKRTSAE3 个月前
快速上手SHELL脚本基础及变量与运算
linux·运维·服务器·脚本
百锦再3 个月前
安卓无障碍脚本开发全教程
android·手机·脚本·开发·mobile·phone·无障碍
时光飞逝的日子3 个月前
linux下编写shell脚本一键编译源码
linux·shell·脚本·编译
中草药z4 个月前
【Docker】Docker拉取部分常用中间件
运维·redis·mysql·docker·容器·nacos·脚本
Linux运维技术栈4 个月前
深入理解 CICD 与 Jenkins 流水线:从原理到实践
运维·ci/cd·jenkins·脚本·流水线
伊织code5 个月前
uv - Guides 指南 [官方文档翻译]
python·脚本·pip·uv·依赖·guide
lj想拿50w5 个月前
脚本学习(1)验证目录自动化生成脚本
python·脚本·ic验证
rosqin6 个月前
Photoshop EXIF 脚本
脚本·photoshop·exif