doracoin ha revisionato questo gist . Vai alla revisione
1 file changed, 88 insertions
rename_img_files.py(file creato)
| @@ -0,0 +1,88 @@ | |||
| 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() | |
Più nuovi
Più vecchi