我的整理QQ表情用的python批处理脚本

1.读取所有文件的md5码,并以此将文件重命名

#!/usr/bin/python
# -*- coding:utf-8 -*-
# Filename:getmd5&rename.py

import os
import sys
import hashlib
import shutil


# 修改文件名以命名md5
def getFileMd5():
    thisPath = "E:\\表情包\\Image"  # 获取的路径
    fileList = os.listdir(thisPath)
    for i in fileList:
        upPath = thisPath + '\\' + i  # 获取目录下文件路径
        fileMd5 = hashlib.md5()  # 计算MD5
        fileType = os.path.splitext(upPath)[-1].lower()  # 获取文件后缀名
        fileOpen = open(upPath, 'rb')
        fileMd5.update(fileOpen.read())
        fileOpen.close()
        fileMd5List = fileMd5.hexdigest()
        try:
            os.rename(upPath, os.path.join(thisPath, fileMd5List + fileType))  # 把目录下的文件以md5重命名
            print('已更改: ' + fileMd5List + fileType)
        except IOError:
            print("error")
            errorPath = 'E:\表情包\Error'
            shutil.move('E:\表情包\Image', errorPath)
            print("Error Files was moved")


# main
if __name__ == "__main__":
    getFileMd5()

2.根据文件名第一个字母将文件移动到各自的文件夹

#!/usr/bin/python
# -*- coding:utf-8 -*-
# Filename:moveFile.py

import os
import sys
import shutil

thisPath = "E:\\表情包\\Image"  # 获取的路径
fileList = os.listdir(thisPath)
for i in fileList:
    upPath = thisPath + '\\' + i  # 获取目录下文件路径
    fileName0 = os.path.splitext(upPath)[0]  # 获取文件名
    fileType = os.path.splitext(upPath)[-1].lower()  # 获取文件后缀名
    fileName = fileName0 + fileType
    fileName1 = fileName[14]  # 文件的首字母
    # oldDir = fileName[0:14]
    newDir0 = "E:\\表情包\\整理\\0\\" + fileName[14:]
    newDir1 = "E:\\表情包\\整理\\1\\" + fileName[14:]
    newDir2 = "E:\\表情包\\整理\\2\\" + fileName[14:]
    newDir3 = "E:\\表情包\\整理\\3\\" + fileName[14:]
    newDir4 = "E:\\表情包\\整理\\4\\" + fileName[14:]
    newDir5 = "E:\\表情包\\整理\\5\\" + fileName[14:]
    newDir6 = "E:\\表情包\\整理\\6\\" + fileName[14:]
    newDir7 = "E:\\表情包\\整理\\7\\" + fileName[14:]
    newDir8 = "E:\\表情包\\整理\\8\\" + fileName[14:]
    newDir9 = "E:\\表情包\\整理\\9\\" + fileName[14:]
    newDira = "E:\\表情包\\整理\\a\\" + fileName[14:]
    newDirb = "E:\\表情包\\整理\\b\\" + fileName[14:]
    newDirc = "E:\\表情包\\整理\\c\\" + fileName[14:]
    newDird = "E:\\表情包\\整理\\d\\" + fileName[14:]
    newDire = "E:\\表情包\\整理\\e\\" + fileName[14:]
    newDirf = "E:\\表情包\\整理\\f\\" + fileName[14:]
    newDirg = "E:\\表情包\\整理\\g\\" + fileName[14:]
    newDirh = "E:\\表情包\\整理\\h\\" + fileName[14:]
    newDiri = "E:\\表情包\\整理\\i\\" + fileName[14:]
    newDirj = "E:\\表情包\\整理\\j\\" + fileName[14:]
    newDirk = "E:\\表情包\\整理\\k\\" + fileName[14:]
    newDirl = "E:\\表情包\\整理\\l\\" + fileName[14:]
    newDirm = "E:\\表情包\\整理\\m\\" + fileName[14:]
    newDirn = "E:\\表情包\\整理\\n\\" + fileName[14:]
    newDiro = "E:\\表情包\\整理\\o\\" + fileName[14:]
    newDirp = "E:\\表情包\\整理\\p\\" + fileName[14:]
    newDirq = "E:\\表情包\\整理\\q\\" + fileName[14:]
    newDirr = "E:\\表情包\\整理\\r\\" + fileName[14:]
    newDirs = "E:\\表情包\\整理\\s\\" + fileName[14:]
    newDirt = "E:\\表情包\\整理\\t\\" + fileName[14:]
    newDiru = "E:\\表情包\\整理\\u\\" + fileName[14:]
    newDirv = "E:\\表情包\\整理\\v\\" + fileName[14:]
    newDirw = "E:\\表情包\\整理\\w\\" + fileName[14:]
    newDirx = "E:\\表情包\\整理\\x\\" + fileName[14:]
    newDiry = "E:\\表情包\\整理\\y\\" + fileName[14:]
    newDirz = "E:\\表情包\\整理\\z\\" + fileName[14:]

    # print(oldDir)
    # print(newDir)
    # print(fileName1)
    # print(fileName1.startswith('0'))

    # # for i in range(10):
    if (fileName1.startswith('0')):
        shutil.move(fileName, newDir0)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('1')):
        shutil.move(fileName, newDir1)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('2')):
        shutil.move(fileName, newDir2)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('3')):
        shutil.move(fileName, newDir3)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('4')):
        shutil.move(fileName, newDir4)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('5')):
        shutil.move(fileName, newDir5)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('6')):
        shutil.move(fileName, newDir6)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('7')):
        shutil.move(fileName, newDir7)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('8')):
        shutil.move(fileName, newDir8)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('9')):
        shutil.move(fileName, newDir9)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('a')):
        shutil.move(fileName, newDira)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('b')):
        shutil.move(fileName, newDirb)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('c')):
        shutil.move(fileName, newDirc)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('d')):
        shutil.move(fileName, newDird)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('e')):
        shutil.move(fileName, newDire)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('f')):
        shutil.move(fileName, newDirf)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('g')):
        shutil.move(fileName, newDirg)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('h')):
        shutil.move(fileName, newDirh)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('i')):
        shutil.move(fileName, newDiri)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('j')):
        shutil.move(fileName, newDirj)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('k')):
        shutil.move(fileName, newDirk)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('l')):
        shutil.move(fileName, newDirl)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('m')):
        shutil.move(fileName, newDirm)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('n')):
        shutil.move(fileName, newDirn)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('o')):
        shutil.move(fileName, newDiro)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('p')):
        shutil.move(fileName, newDirp)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('q')):
        shutil.move(fileName, newDirq)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('r')):
        shutil.move(fileName, newDirr)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('s')):
        shutil.move(fileName, newDirt)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('t')):
        shutil.move(fileName, newDirt)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('u')):
        shutil.move(fileName, newDiru)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('v')):
        shutil.move(fileName, newDirv)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('w')):
        shutil.move(fileName, newDirw)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('x')):
        shutil.move(fileName, newDirx)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('y')):
        shutil.move(fileName, newDiry)
        print('已移动: ' + fileName[14:])
    elif (fileName1.startswith('z')):
        shutil.move(fileName, newDirz)
        print('已移动: ' + fileName[14:])
    else:
        print('Error' + fileName[14:])

其他

作为新手写的脚本,望各位提宝贵的意见

添加新评论