LoginSignup
0
0

More than 3 years have passed since last update.

スクショなどのファイル名のスペースやカッコなどをPythonで一括置換した

Last updated at Posted at 2021-04-19

モチベーション

macOSやLinuxでスクリーンショットを撮ると、スクリーンショット 20××-11-22 33.44.55.pngScreenshot from 2021-04-19 14-19-39.pngなどのようにスペースが含まれてしまう。

また同じ名前のファイルをダウンロードすると、test (1).pngなどのようにスペースとかっこが含まれていて、コマンドでの操作が非常にやりにくい。

そこで、そのディレクトリにあるファイル名のスペースやかっこなどの記号を一括置換するプログラムをPythonで書きました。これを~/Downloadsやら~/Picturesやら~/Desktopのディレクトリに1個置いておくだけで、かなり便利になると思います。(updated)

なおmacOSの場合の、スクリーンショット 20××-11-22 33.44.55.pngスクリーンショットという日本語の部分は「Macのスクリーンショットの保存先と名前を変更する」でも排除できます。

コード

import os
import shutil


def get_filenames(path):
    files_and_dir = os.listdir(path) # get a list of names of files and directories
    files = [i for i in files_and_dir if os.path.isfile(os.path.join(path, i))] # a list of names of only files
    return files


def replace_a_with_b(path, a, b):
    files = get_filenames(path)
    for f in files:              # For example, if f="test 1.pdf", we want to replace a=" " with b="_".
        if a in f:               #
            g = f.replace(a, b)  # g = "test_1.pdf"
            shutil.move(f, g)    # mv 'test 1.pdf' test_1.pdf


def main():
    path = "." # current directory
    replace_a_with_b(path, " ", "_")
    replace_a_with_b(path, "(", "")
    replace_a_with_b(path, ")", "")
    # ここに追加したいものを書いてください


if __name__ == "__main__":
    main()

Ubuntuの~/Picturesで実行

Pictures $ ls
total 360K
-rw-rw-r-- 1 <user> <group> 158K Apr 18 22:17 'Screenshot from 2021-04-18 22-17-40.png'
-rw-rw-r-- 1 <user> <group> 140K Apr 19 14:19 'Screenshot from 2021-04-19 14-19-39.png'
-rw-r--r-- 1 <user> <group>  783 Apr 19 23:24  renamefiles.py

Pictures $ python renamefiles.py

Pictures $ ls
total 360K
-rw-rw-r-- 1 <user> <group> 158K Apr 18 22:17 Screenshot_from_2021-04-18_22-17-40.png
-rw-rw-r-- 1 <user> <group> 140K Apr 19 14:19 Screenshot_from_2021-04-19_14-19-39.png
-rw-r--r-- 1 <user> <group>  783 Apr 19 23:24 renamefiles.py

問題点

置換したい文字を追加すればするほどforの回数が多くなってしまって、あまり効率がよくない気がします。「もっとこうしたらいいよ」というのがありましたらぜひ教えてください。

改善

パス名を引数として渡すことで、コマンド的に使えるようにしました。

renamefiles.py
import os
import shutil
import sys


def get_filenames(path):
    files_and_dir = os.listdir(path) # get a list of names of files and directories
    files = [i for i in files_and_dir if os.path.isfile(os.path.join(path, i))] # a list of names of only files
    return files


def replace_a_with_b(path, a, b):
    files = get_filenames(path)
    for f in files:              # For example, if f="test 1.pdf", we want to replace a=" " with b="_".
        if a in f:               #
            g = f.replace(a, b)  # g = "test_1.pdf"
            shutil.move(f, g)    # mv 'test 1.pdf' test_1.pdf


def main():
    if len(sys.argv)!=2:
        print("Usage: renamefiles <path_to_files>")
        sys.exit(1)
    else:
        # path = "." # current directory
        path = sys.argv[1]
        replace_a_with_b(path, " ", "_")
        replace_a_with_b(path, "(", "")
        replace_a_with_b(path, ")", "")


if __name__ == "__main__":
    main()

これをホームディレクトリなどに置いておき、~/.bashrcに以下を追加してください。

alias renamefiles="python ~/renamefiles.py"

終わったら

source ~/.bashrc

を実行すれば準備完了。

実行例は

$ renamefiles /path/to/files
$
0
0
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
0