Last active
September 25, 2021 09:34
-
-
Save curegit/4d12d680511764ff823c8c26caf8606e to your computer and use it in GitHub Desktop.
Twitter から保存した画像を時系列順にリネームするスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import os.path | |
import sys | |
import glob | |
def map(char): | |
a = ord(char) | |
if 48 <= a <= 57: | |
return "~" + char | |
elif char == "-": | |
return "~~" | |
elif char == "_": | |
return "~~~" | |
else: | |
return char | |
def key(str): | |
return "".join([map(c) for c in str]) | |
def new_filepath(path, new, suffix="+"): | |
root, ext = os.path.splitext(path) | |
head, tail = os.path.split(root) | |
path = os.path.join(head, new) + ext | |
if os.path.lexists(path): | |
return new_filepath(path, new + suffix, suffix) | |
return path | |
ls = [f for f in glob.glob(os.path.join(sys.argv[1], "*")) if os.path.isfile(f)] | |
new = sorted(ls, key=key) | |
for i, f in enumerate(new): | |
n = new_filepath(f, str(i)) | |
os.rename(f, n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment