2
2

More than 3 years have passed since last update.

大量に取ったスクリーンショットの名前を変更するのがめんどうだったのでPythonで変更した

Last updated at Posted at 2020-07-23

目的

Macを使っています。スクリーンショットを20枚くらい保存しましたが、それぞれ「スクリーンショット 年月日時刻.png」という名前で保存されます。
これをまとめて「~~001.png」のようにしたかったため、Pythonで以下のプログラムを書きました。

概要

python 本ファイル 'A' 'B' N

A:変換するファイル一覧。ワイルドカードを使う(例:*.png)。
B:変更後ファイル名。拡張子は継承される(出力:B001.png)。
N:連番の開始番号(整数値)。何もいれないと0から開始する。

ファイルのソートは名前順です。
Bはクオーテーションで囲わなくてもいいですが、Aはクオーテーションで囲ってください。

ソースコード

何かあったときのために、27行目をコメントアウトしています。外してください。

import os, glob, sys, pprint
args = sys.argv

# とりあえず、Aでヒットしたファイルを出力する
dirlist = sorted(glob.glob(args[1]))
pprint.pprint(dirlist)

#Nの確認
count = 0
if len(args) >= 4:
    count = int(args[3])

#出力例の表示と「本当に変更しますか?(y/n)」
ext = dirlist[0][dirlist[0].rfind('.')+1:]
ncount = '{0:03d}'.format(count)
print('change to {}{}.{} ... ? (y/n)'.format(args[2],ncount,ext))
S = input()
if S != 'y':
    exit()

#実際にファイル名を変更
for i, name in enumerate(dirlist):
    ext = name[name.rfind('.')+1:]
    ncount = '{0:03d}'.format(count+i)
    hdir = name[0:name.rfind('/')+1]
    newname = '{}{}{}.{}'.format(hdir,args[2],ncount,ext)
    # os.rename(name, newname)
    print('change {} to {}'.format(name,newname))
2
2
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
2
2