LoginSignup
0
0

More than 3 years have passed since last update.

【環境構築から】Python でファイル名の一括変更

Last updated at Posted at 2019-10-15

環境構築

venv を使用するのが一番軽くて簡単そう。

Terninal で仮装環境を作るディレクトリに移動

仮装環境の新規作成

python3 -m venv [new_env_name]   

仮装環境起動

. [new_env_name]/bin/activate

起動できてたら $の左に([new_env_name])が表示される

Pythonファイル

先ほどのnew_env_name直下に 以下のPythonファイルを作成

import glob
import os

path =  '/Users/~~~/'   # 変更したいファイルが存在するディレクトリ
baseName = ''      #変更後の名前 (この後ろに連番+拡張子がつく)
pre_extension = ''       #存在するファイルの拡張子
post_extension = '.png'  #変更後の拡張子
counter = 1         #連番スタート値

joinedPath = path + '*' + pre_extension
flist = glob.glob(joinedPath)




for f in flist:
    newName = baseName + str(counter).zfill(2) + post_extension
    os.rename(f, path + newName)
    counter += 1



今回は 01~~99 になるようにzfill(2)と設定したがそこは臨機応変に。

0
0
0

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
0