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()