格式字幕格式脚本

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

相关推荐
k4m7v2pz9 天前
ThinkPad E490 风扇 “转两秒就停“ 实录:迟滞曲线 + 最短运转时间
linux·脚本·archlinux·thinkpad·风扇·散热
五_谷_丰_登17 天前
cmake脚本学习——持续更新
脚本·cmake
szephyr18 天前
用 Python 写自动化脚本:定时任务、文件批处理、自动出报表
python·自动化·pandas·脚本·定时任务
2601_962299881 个月前
Linux执行Python脚本方法
linux·python·脚本·解释器·shebang
DolphinDB1 个月前
循环加速最高达 3 倍!DolphinDB 动态脚本优化免费开启,零代码改造即可投入生产
脚本·时序数据库·dolphindb
2601_962295331 个月前
使用python实现一个浏览器自动化的脚本
python·脚本·rpa·浏览器自动化·操作
2601_962299881 个月前
Linux下运行Python脚本
linux·python·ubuntu·脚本·命令
YuSun_WK1 个月前
shell脚本
shell·脚本
寒水馨2 个月前
macOS下载、安装PowerShell-v7.6.4(附安装包powershell-7.6.4-osx-arm64.pkg)
macos·自动化·跨平台·shell·脚本·命令行·powershell
逻极2 个月前
Shell脚本实战:从“能跑”到“高效”的3个关键跃升
linux·服务器·shell·脚本