1. 批量修改文件名
例子: 把b站下載的長文件名替換為短的
'''
說明: 去掉文件名中共同的部分
'''
import os
# 修改文件
def rename(data_dir: str, is_loop: bool, old: str, new: str):
fileList = os.listdir(data_dir)
for file_name in fileList:
full_file_name = os.path.join(data_dir, file_name)
# 遍歷所有文件夾中的文件
if os.path.isdir(full_file_name):
if is_loop:
rename(full_file_name, is_loop, old, new)
else:
continue
new_name = file_name.replace(old, new, -1)
full_new_name = os.path.join(data_dir, new_name)
if full_file_name == full_new_name:
continue
print(full_file_name)
print(f'替換 {full_new_name}')
os.rename(full_file_name, full_new_name)
pass
# 最外層的文件夾
data_path = r'2021年最新爬蟲+反爬+js逆向(配套完整項(xiàng)目)_'
old = ''
new = ''
rename(data_path, True, old, new)
2. 網(wǎng)盤分享通過:
替換: 網(wǎng)盤會(huì)審核文件名, 所以,干脆所有文件名都只用數(shù)字表示,順便給自己網(wǎng)站打一波廣告
0400 第402章 決勝的關(guān)鍵.m4a => 0400{discuz.elandcloud.com}.m4a
import os
import re
def rename(data_dir: str, ptn: re.Pattern, new_repl: str, is_loop: bool):
fileList = os.listdir(data_dir)
for file_name in fileList:
full_file_name = os.path.join(data_dir, file_name)
# 遍歷所有文件夾中的文件
if os.path.isdir(full_file_name):
if is_loop:
rename(full_file_name, ptn, is_loop)
else:
continue
new_name = ptn.sub(new_repl, file_name)
full_new_name = os.path.join(data_dir, new_name)
os.rename(full_file_name, full_new_name)
pass
data_path = r'D:\1.source\pythonpath\xmly-paid\data\瑯琊榜'
ptn = re.compile(r'(\d+).*?(\.m4a)')
# 選擇要保留的組,\g<1>表示第1組(就是前面的數(shù)字),\g<2>表示第2組(就是.m4a)
new_repl = r'\g<1>{discuz.elandcloud.com}\g<2>'
rename(data_path, ptn, new_repl, True)
審核編輯:劉清
-
python
+關(guān)注
關(guān)注
57文章
4877瀏覽量
90082
發(fā)布評(píng)論請(qǐng)先 登錄
飛凌嵌入式ElfBoard-文件的時(shí)間屬性之utime
飛凌嵌入式ElfBoard-文件的時(shí)間屬性
Termux中調(diào)試圣誕樹Python代碼
飛凌嵌入式ElfBoard-標(biāo)準(zhǔn)IO接口之打開文件
飛凌嵌入式ElfBoard-Linux系統(tǒng)基礎(chǔ)入門-文件操作相關(guān)shell命令
termux輸出Hello termux
如何移除意外添加到項(xiàng)目中或不再需要的板級(jí)標(biāo)注
怎么導(dǎo)出python邊緣計(jì)算中的APP,想進(jìn)行修改又找不到源碼?
harmony-utils之FileUtil,文件相關(guān)工具類
迅為RK3568開發(fā)板Dev-Eco studio 的界面布局-導(dǎo)航欄-代碼編輯區(qū)
基于FPGA搭建神經(jīng)網(wǎng)絡(luò)的步驟解析
python之怎樣去批量修改文件名呢
評(píng)論