LoginSignup
0
1

More than 1 year has passed since last update.

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

Posted at

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

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

0
1
1

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
  3. You can use dark theme
What you can do with signing up
0
1