Created
May 13, 2020 17:15
-
-
Save QuietMisdreavus/de2c5277e45533e5c9181eda1c70606a to your computer and use it in GitHub Desktop.
old script to import files into a wallpaper directory
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
#!/usr/bin/env python3 | |
import os | |
from os.path import splitext | |
import hashlib | |
pictypes = ".png", ".jpg", ".bmp", ".gif" | |
ls = os.listdir(".") | |
def lsmd5(): | |
ret = [] | |
for name in ls: | |
_, ext = splitext(name) | |
if ext in pictypes: | |
with open(name, "rb") as file: | |
m = hashlib.md5() | |
m.update(file.read()) | |
ret.append((name, m.hexdigest() + ext)) | |
return ret | |
def massrename(list): | |
for names in list: | |
if names[1] not in ls: | |
try: | |
os.rename(names[0], names[1]) | |
except OSError: | |
print(names[0] + ", " + names[1] + " rename failed") | |
if __name__ == "__main__": | |
massrename(lsmd5()) | |
ls = os.listdir(".") | |
for file in ls: | |
_, ext = splitext(file) | |
if ext in pictypes: | |
try: | |
os.rename(file, "../"+file) | |
except OSError: | |
print(file + " move failed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment