LoginSignup
1

posted at

Pythonでフォルダ名やファイル名を一括変更

全てのファイルやフォルダの名前の後ろに文字を加える

image.png

image.png

import os
dir_path = "./dirname"
dir_list = os.listdir(dir_path)
for i in range(len(dir_list)):
    new_file_name = dir_list[i] + "様"
    os.rename(os.path.join(dir_path,dir_list[i]),os.path.join(dir_path,new_file_name))

ファイル名やフォルダ名から特定の文字だけを変更・削除

image.png

image.png

import os
import glob

before_word = "様"
after_word = ""

files = glob.glob('*'+ before_word +'*')
for before_file_name in files:
    after_file_name = before_file_name.replace(before_word,after_word)
    os.rename(before_file_name,after_file_name)

もっといいやり方があったら教えてくださいm(_ _)m

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1