Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created March 14, 2024 06:59
Show Gist options
  • Save baba-s/41cbdcd56d9f584137f9603629cc9fd2 to your computer and use it in GitHub Desktop.
Save baba-s/41cbdcd56d9f584137f9603629cc9fd2 to your computer and use it in GitHub Desktop.
import os
import re
def remove(folder_path):
# 指定したフォルダ内のファイルをリストアップ
files = os.listdir(folder_path)
# 正規表現パターン
pattern = re.compile(r'Hoge', re.IGNORECASE) # 大文字小文字を区別しないで検索
# ファイル名を変更
for file_name in files:
# ファイル名から「Hoge」を削除
new_file_name = re.sub(pattern, '', file_name)
# フルパス
old_full_path = os.path.join(folder_path, file_name)
new_full_path = os.path.join(folder_path, new_file_name)
# ファイル名を変更
os.rename(old_full_path, new_full_path)
# フォルダのパス
folder_path = '/path/to/your/folder'
# 「Hoge」を削除
remove(folder_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment