本節目錄
常用函式一:批量檔案重新命名
# -*- coding: utf-8 -*-執行結果"""datetime : 2021/02/08 10:29
author : zhangyafei
description: 批量檔案重新命名
"""import os
origin_path = path
dir_path, path = os.path.split(path)
path, file_type_suffix = path.rsplit('.', maxsplit=1) if file_type else (path, none)
if prefix:
path = f''
if suffix:
path = f''
if replace:
for key, val in replace:
path = path.replace(key, val)
elif remove:
if prefix and path.startswith(prefix):
path = path[len(prefix):]
if suffix:
path = path[:-len(suffix)]
if replace:
for key, val in replace:
path = path.replace(key, val)
elif replace:
for key in replace:
path = path.replace(key, replace[key])
if file_type:
path = f'.'
if dir_path:
path = os.path.join(dir_path, path)
os.rename(origin_path, path)
print(f' rename to success.')
"""批量檔案重新命名
:param dir_path: 資料夾名稱
:param only_file: 是否僅修改檔案
:param only_dir: 是否僅修改資料夾
:param prefix: 字首
:param suffix: 字尾
:param replace: 替換
:param remove: 刪除
:param file_type: 檔案型別
:return:
"""if only_file:
for base_path, folders, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(base_path, file)
elif only_dir:
for base_path, folders, files in os.walk(dir_path):
for folder in folders:
folder_path = os.path.join(base_path, folder)
else:
for base_path, folders, files in os.walk(dir_path):
for folder in folders:
folder_path = os.path.join(base_path, folder)
for file in files:
file_path = os.path.join(base_path, file)
if __name__ == '__main__':
# batch_file_rename('data', replace=)
batch_file_rename('data', prefix='[張亞飛]', remove=true)
Python常用功能函式
1.字串反轉 字串反轉 string 字串變數 staticmethod def str reverse string result string 1 return result 2.刪除首尾指定的字元 刪除首尾指定的字元 string 字串變數 rm 要刪除的字元,預設為空格 staticmeth...
python 常用功能
sintance和type class foo object pass class bar foo pass obj bar isinstance用於判斷,物件是否是指定類的例項 錯誤的 isinstance用於判斷,物件是否是指定類或其派生類的例項 isinstance不精準 print isin...
jquery常用功能函式
1.運算元組和物件 主要包括元素的遍歷,篩選,合併等 1 遍歷each 格式 each object,fn object是要遍歷的物件。fn是遍歷所有物件所要執行的函式,可以接受兩個引數 1.陣列物件的屬性或者元素序號 2.屬性或者元素的值 例如 html 輸出每個列表項的值jquery butto...