LoginSignup
0
0

More than 3 years have passed since last update.

【Twitterの紳士たちへ】.jpg-largeを.jpgに一括変換するscriptを書きました

Last updated at Posted at 2019-11-17

What is?

前略

あなたがTwitterでヱロ画像を収集していることを俺は知っている...。
そして.jpg-largeを毎回ハンドで.jpgに直して保存していることも知っている...(してなかったらすみません)。

そんな! あなたに!!朗報です!!!

このスクリプトで崇高なるヱロ画像のファイル名を一括変換しましょう!!!!!

ダウンロードはこちらから

https://github.com/ykaito39/useful-script-for-photo/tree/master/rename_jpg-XXX_to_jpg

使い方

$python rename_jpg-XXX_to_jpg.py

※Windows環境でのみ確認

基本的に.jpg-largeがあるディレクトリにコピーしてダブルクリックで実行すれば実行可能です。

中身解説

めちゃくちゃ簡単なスクリプトで、自分が初めて作ったまともに使えるスクリプトです。
2年くらい前に作ったものですが、バリバリ現役で使えてます。

おまじない

# coding: UTF-8
import sys
import os

対象のパス指定

if len(sys.argv) == 2:
    DIR == sys.argv[1]
else:
    DIR = './'

本当はOS毎に分けて書いてたんですけど、結局ダウンロード直下にコピーして実行するほうが一番直感的でわかりやすいなーと思って修正しました。

本体

#ディレクトリ内のファイル一覧を取得
#DIRが不正ならここで終了する
try:
    file_list = os.listdir(DIR)
except os.error:
    exit(-1)

for file in file_list:
    print(file)
    if file[-6:] == '_large':
        #変更対象のフルパスを取得
        full_path = os.path.join(DIR, file)

        #ファイル名から末尾の_largeを消す
        replaced_path = file[:-6] + ''
        replaced_full_path = os.path.join(DIR, replaced_path)

        #ファイル名の書き換え
        os.rename(full_path, replaced_full_path)
        print('[REPLACE]  ' + file + ' >> ' + replaced_path)

ディレクトリの内容一覧を取得してlistのスライスでファイル名末尾に-largeがついていたら消しているだけの処理内容です。
例外処理も適当で、大した内容じゃなくてすみません。

開発環境

Python3.6
Windows10 pro x64 1903

連絡先

@ykaito39

Thanks

見てくれてありがとう...

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