rename_img_files.py
· 2.2 KiB · Python
Orginalformat
import getopt
import os
import sys
# 建议用`PyInstaller`打包成单个exe文件,方便使用
# 执行 `pip3 install pyinstaller`
# 然后执行 `pyinstaller -F yourscript.py` 将程序打包成单个文件
version = '1.7'
release_date = '2019-01-07 14:17:49'
cwd = os.getcwd()
files = os.listdir(cwd)
match_list = [
"TIM图片_",
"TIM图片-",
"TIM图片",
"TIM截图_",
"TIM截图-",
"TIM截图",
"QQ图片_",
"QQ图片-",
"QQ图片",
"QQ截图_",
"QQ截图-",
"QQ截图",
"微信图片_",
"微信图片-",
"微信图片",
"微信截图_",
"微信截图-",
"微信截图",
]
def do_replace():
for f in files:
cur_path = cwd + os.path.sep + f
if not os.path.isdir(cur_path) and is_image(f):
for match_str in match_list:
if f.startswith(match_str):
tar = cwd + os.path.sep + f.replace(match_str, "")
if not os.path.exists(tar):
do_rename(cur_path, tar)
break
else:
print("%s 同名文件已存在,跳过." % cur_path)
continue
print("处理完成")
def is_image(filename):
if (filename.endswith("jpg")
| filename.endswith("jpeg")
| filename.endswith("png")
| filename.endswith("bmp")
| filename.endswith("gif")):
return True
def do_rename(src, target):
os.rename(src, target)
print("`%s` was renamed." % src)
def print_help():
print("欢迎使用 《文件名前缀批量移除》 工具. \n")
print(" " + version + "\n")
print(" " + release_date + "\n")
print(" -m: More prefix, split with 'space' \n")
if __name__ == '__main__':
if sys.argv.__len__() > 1:
opts, args = getopt.getopt(sys.argv[1:], "hm:", ["help=", "more="])
if not opts:
print_help()
sys.exit(0)
for op, value in opts:
if op == "-m":
print(op + "= " + value)
else:
print_help()
sys.exit(0)
else:
do_replace()
| 1 | import getopt |
| 2 | import os |
| 3 | import sys |
| 4 | |
| 5 | # 建议用`PyInstaller`打包成单个exe文件,方便使用 |
| 6 | # 执行 `pip3 install pyinstaller` |
| 7 | # 然后执行 `pyinstaller -F yourscript.py` 将程序打包成单个文件 |
| 8 | |
| 9 | version = '1.7' |
| 10 | release_date = '2019-01-07 14:17:49' |
| 11 | |
| 12 | cwd = os.getcwd() |
| 13 | |
| 14 | files = os.listdir(cwd) |
| 15 | |
| 16 | match_list = [ |
| 17 | "TIM图片_", |
| 18 | "TIM图片-", |
| 19 | "TIM图片", |
| 20 | "TIM截图_", |
| 21 | "TIM截图-", |
| 22 | "TIM截图", |
| 23 | "QQ图片_", |
| 24 | "QQ图片-", |
| 25 | "QQ图片", |
| 26 | "QQ截图_", |
| 27 | "QQ截图-", |
| 28 | "QQ截图", |
| 29 | "微信图片_", |
| 30 | "微信图片-", |
| 31 | "微信图片", |
| 32 | "微信截图_", |
| 33 | "微信截图-", |
| 34 | "微信截图", |
| 35 | ] |
| 36 | |
| 37 | |
| 38 | def do_replace(): |
| 39 | for f in files: |
| 40 | cur_path = cwd + os.path.sep + f |
| 41 | if not os.path.isdir(cur_path) and is_image(f): |
| 42 | for match_str in match_list: |
| 43 | if f.startswith(match_str): |
| 44 | tar = cwd + os.path.sep + f.replace(match_str, "") |
| 45 | if not os.path.exists(tar): |
| 46 | do_rename(cur_path, tar) |
| 47 | break |
| 48 | else: |
| 49 | print("%s 同名文件已存在,跳过." % cur_path) |
| 50 | continue |
| 51 | print("处理完成") |
| 52 | |
| 53 | |
| 54 | def is_image(filename): |
| 55 | if (filename.endswith("jpg") |
| 56 | | filename.endswith("jpeg") |
| 57 | | filename.endswith("png") |
| 58 | | filename.endswith("bmp") |
| 59 | | filename.endswith("gif")): |
| 60 | return True |
| 61 | |
| 62 | |
| 63 | def do_rename(src, target): |
| 64 | os.rename(src, target) |
| 65 | print("`%s` was renamed." % src) |
| 66 | |
| 67 | |
| 68 | def print_help(): |
| 69 | print("欢迎使用 《文件名前缀批量移除》 工具. \n") |
| 70 | print(" " + version + "\n") |
| 71 | print(" " + release_date + "\n") |
| 72 | print(" -m: More prefix, split with 'space' \n") |
| 73 | |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | if sys.argv.__len__() > 1: |
| 77 | opts, args = getopt.getopt(sys.argv[1:], "hm:", ["help=", "more="]) |
| 78 | if not opts: |
| 79 | print_help() |
| 80 | sys.exit(0) |
| 81 | for op, value in opts: |
| 82 | if op == "-m": |
| 83 | print(op + "= " + value) |
| 84 | else: |
| 85 | print_help() |
| 86 | sys.exit(0) |
| 87 | else: |
| 88 | do_replace() |