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逆向(配套完整項目)_'
old = ''
new = ''
rename(data_path, True, old, new)
2. 網(wǎng)盤分享通過:
替換: 網(wǎng)盤會審核文件名, 所以,干脆所有文件名都只用數(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)注
56文章
4827瀏覽量
86778
發(fā)布評論請先 登錄
迅為RK3568開發(fā)板Dev-Eco studio 的界面布局-導(dǎo)航欄-代碼編輯區(qū)
IAR無法跳轉(zhuǎn)定義,系統(tǒng)庫文件文件名后有[RO]是怎么回事?
dlpc3433怎樣修改固件會將開機時顯示的為無啟動畫面且為外部輸入模式呢?
如何將python文件導(dǎo)入到ROS系統(tǒng)中

hyper-v 文件,Hyper-V文件管理:高效操作指南

嵌入式學(xué)習(xí)-飛凌嵌入式ElfBoard ELF 1板卡-初識設(shè)備樹之Makefile修改
飛凌嵌入式ElfBoard ELF 1板卡-初識設(shè)備樹之Makefile修改
Labview文件路徑中空格變成問號導(dǎo)致文件無法打開
Linux文件查找
嵌入式 學(xué)習(xí)-飛凌嵌入式ElfBoard ELF 1板卡-shell腳本編寫之輸入輸出重定向
LMH7322怎樣去改善輸出波形呢 ?
常見的shell命令之文件操作相關(guān)命令
使用Python批量連接華為網(wǎng)絡(luò)設(shè)備
PDF文件批量打印源代碼
如何修改buildroot和debian文件系統(tǒng)

評論