1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| ''' @file :test.py @Time :2021-08-20 02:52:51 @Author :Karcyril @Software :Visual Studio Code '''
import os import re
def main(): while(removeFiles(input('Q:退出\nR:修改文件名\n直接输入路径批量修改压缩包后缀\n请输入:'))): pass
def removeFiles(var): if var.upper() == 'Q': return False
if var.upper() == 'R': mRename() return True
path = var.replace('\\', '/') files = os.listdir(path) for file in files: matchObj = re.match('(.*?)\.(.*)', file) newFile = '' if(matchObj and os.path.isfile(os.path.join(path, file))): newFile = matchObj.group(1)+'.zip' else: newFile = file+'.zip'
os.rename(os.path.join(path, file), os.path.join(path, newFile))
return True
def mRename(): path = input('请输入文件夹路径:').replace('\\', '/') files = os.listdir(path) index = 1 for file in files: matchObj = re.match('(.*?)\.(.*)', file) if(matchObj and os.path.isfile(os.path.join(path, file))): newFile = 'btkrc.com[%d]' % (index)+'.'+matchObj.group(2) os.rename(os.path.join(path, file), os.path.join(path, newFile)) index += 1
if __name__ == "__main__": main()
|