note1
以下程序尚未考虑健壮性
只是以添加后缀为主要目的,按照自然数序来作为新名称
关于以下使用的路径,根据自身情况替换
note2
- 不可以在新文件名中包含非法字符
- 基于python的重命名实现:描述文件时,要么使用绝对路径,要么使用os.chdir()切换路径后直接使用名字,否则无法重命名
- 基于powershell的重命名实现:代码可以更加简介,这是系统shell的优势,但是该语言没有像python那样普及
基于python实现:
以<时间+n>为文件名
提取并重命名win10/11的锁屏图
| import os |
| import os.path as op |
| import time |
| dirName="C:/Users/cxxu_11/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets" |
| def renameFiles(dirName='.'): |
| items=os.listdir(dirName) |
| index=0 |
| |
| os.chdir(dirName) |
| print(os.getcwd()) |
| for item in items: |
| index+=1 |
| |
| time_string = time.strftime( |
| '%Y-%m-%d %H-%M-%S', time.localtime(time.time())) |
| time_string=time_string+" "+str(index) |
| newName= time_string+'.jpg' |
| if not op.exists(newName): |
| os.rename(item,newName) |
| |
| print(item) |
| |
| |
| if __name__=="__main__": |
| |
| print("test") |
| renameFiles(dirName) |
以00,01,02,03为文件名
| import os |
| import os.path as op |
| dirName="C:/Users/cxxu/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets" |
| def renameFiles(dirName='.'): |
| items=os.listdir(dirName) |
| index=1 |
| |
| os.chdir(dirName) |
| for item in items: |
| index+=1 |
| newName=str(index)+".jpg" |
| if not op.exists(newName): |
| os.rename(item,newName) |
| |
| print(item) |
| |
| |
| if __name__=="__main__": |
| renameFiles(dirName) |
以00_,01_,02_,…为前缀开头的防止重名的版本
| import os |
| import os.path as op |
| from sys import argv, setprofile |
| import time |
| """ 使用说明,重命名文件是高危操作,为了安全起见,您可以将要被处理的文件做一个备份 |
| 其次,您可以复制文件中的一部分来测试您的代码的可行性 |
| 最后,如果代码中通过保守的预览代码代替实际的重命名操作也是不错的选择(待预览效果符合预期后执行实际的重命名操作是推荐的做法) |
| 本程序只对文件执行重命名(纳入排序计算),文件夹被可以排除,当然,也可以认为修改使得文件夹也一并纳入计算 |
| """ |
| testDir = "d:/repos/learnDatabase_sql/materials" |
| testDir = "D:\\org\\Ecloud\\myMusic\\hitomiShimatani" |
| |
| |
| |
| """ 设计为递归函数,不同深度的递归调用之间只有index会有所不同 """ |
| def noRepeate(item, index, separator, itemList): |
| new_prefix = str(index+1).zfill(2)+separator |
| new_name = new_prefix+item |
| |
| if new_name in itemList: |
| |
| new_prefix = str(index+1).zfill(2)+separator |
| new_name = new_prefix+item |
| return noRepeate(item,index+1,separator,itemList) |
| |
| return new_name |
| |
| |
| def rename_prefix(testDir="."): |
| |
| os.chdir(testDir) |
| itemList = os.listdir(".") |
| currentDirItems=itemList[:] |
| |
| |
| is_beginAscii = False |
| |
| separator = "_" |
| cleanList = [] |
| |
| for item in itemList: |
| is_continue = item[0].isdigit() or item[0] == separator |
| if is_continue: |
| new_name = "" |
| |
| for index, chr in enumerate(item): |
| if chr.isdigit() or chr == separator: |
| continue |
| else: |
| new_name = item[index:] |
| |
| break |
| |
| if op.isfile(item): |
| |
| |
| |
| cleanList.append(new_name) |
| else: |
| ... |
| |
| needNumberList = [] |
| for item in itemList: |
| isNeedNumber = not item[0].isascii() |
| if isNeedNumber: |
| needNumberList.append(item) |
| |
| else: |
| ... |
| itemList = needNumberList |
| |
| for index, item in enumerate(itemList): |
| |
| |
| |
| new_prefix = str(index).zfill(2)+separator |
| new_name = new_prefix+item |
| |
| |
| |
| originName = item |
| previewString = f'😎newName:{new_name}<-😁originName:{originName}' |
| |
| |
| while( (new_name in currentDirItems)): |
| print(f'already exist the new_name:{new_name}🤣') |
| new_prefix = str(index+1).zfill(2)+separator |
| new_name = new_prefix+item |
| |
| |
| print(previewString) |
| os.rename(originName, new_name) |
| |
| |
| if __name__ == "__main__": |
| rename_prefix() |
| |
基于powershell实现
预览效果
执行之前:

执行代码以及之后的结果:

| $i=1 |
| Get-ChildItem * | ForEach-Object { |
| |
| rename $_.Name ($i.toString() + $_.Name); |
| $i += 1 ; |
| } |
执行之前把rename改为write查看预览效果
| $i=1 |
| Get-ChildItem * | ForEach-Object { |
| |
| write ($i.toString() + $_.Name); |
| $i += 1 ; |
| } |
| |
为文件批量添加前缀序号以便于用终端指定/操作文件中文文件
| import os |
| import os.path as op |
| import time |
| """ 使用说明,重命名文件是高危操作,为了安全起见,您可以将要被处理的文件做一个备份 |
| 其次,您可以复制文件中的一部分来测试您的代码的可行性 |
| 最后,如果代码中通过保守的预览代码代替实际的重命名操作也是不错的选择(待预览效果符合预期后执行实际的重命名操作是推荐的做法) |
| |
| 本程序只对文件执行重命名(纳入排序计算),文件夹被可以排除,当然,也可以认为修改使得文件夹也一并纳入计算;当然,您可以将os.isfile()改为os.isdir()来对文件夹进行重命名而干扰文件 |
| """ |
| if __name__ == "__main__": |
| |
| os.chdir("D:\\org\\Ecloud\\myMusic\\hitomiShimatani") |
| itemList = os.listdir(".") |
| is_beginAscii = False |
| |
| separator = "_" |
| cleanList = [] |
| |
| for item in itemList: |
| is_continue = item[0].isdigit() or item[0] == separator |
| if is_continue: |
| new_name = "" |
| |
| for index, chr in enumerate(item): |
| if chr.isdigit() or chr == separator: |
| continue |
| else: |
| new_name = item[index:] |
| |
| break |
| |
| if op.isfile(item): |
| print(new_name) |
| |
| |
| cleanList.append(new_name) |
| |
| else: |
| ... |
| |
| |
| for index, item in enumerate(itemList): |
| |
| |
| |
| new_prefix = str(index).zfill(2) |
| new_prefix = new_prefix+separator |
| new_name = new_prefix+item |
| |
| if op.isfile(item): |
| |
| print(new_name) |
| os.rename(item, new_name) |
| |
终端操作效果
终端点歌:

带分析版本
- 将指定目录(当前目录)下的文件修改为便于终端补全的格式:
- 可以借助powershell/bash调用此脚本
| import os |
| import os.path as op |
| from sys import argv, setprofile |
| import time |
| from venv import create |
| """ 使用说明,重命名文件是高危操作,为了安全起见,您可以将要被处理的文件做一个备份 |
| 其次,您可以复制文件中的一部分来测试您的代码的可行性 |
| 或者,编写一个专门的测试(TDD) |
| 最后,如果代码中通过保守的预览代码代替实际的重命名操作也是不错的选择 |
| (待预览效果符合预期后执行实际的重命名操作是推荐的做法) |
| 本程序只对文件执行重命名(纳入排序计算),文件夹不处理 |
| 使用python脚本,跨平台 |
| ----- |
| 需求分析 |
| - 首先,能够将非字母&数字开头的文件重命名为以指定格式命名的文件:例如,"00_"+文件名 |
| 换句话说,经过函数处理,原先不满足字母|数字开头的文件名改为形如"00_"+文件名的格式 |
| 算法: |
| - 将目录下文件名读入到一个列表保存备用 |
| - 识别并提取出需要做重命名的文件(但是先仅仅做文件名上的变更预览(old->new的格式预览),如果符合预期,则编写相应的IO操作代码) |
| """ |
| |
| |
| |
| |
| """ 设计为递归函数,不同深度的递归调用之间只有index会有所不同 """ |
| def drop_duplicated(item, index, separator, itemList): |
| """用于去重,一般情况下不调用它!""" |
| new_prefix = str(index+1).zfill(2)+separator |
| new_name = new_prefix+item |
| |
| if new_name in itemList: |
| |
| new_prefix = str(index+1).zfill(2)+separator |
| new_name = new_prefix+item |
| return drop_duplicated(item,index+1,separator,itemList) |
| |
| return new_name |
| |
| |
| def rename_prefix(testDir="."): |
| |
| os.chdir(testDir) |
| itemList = os.listdir(".") |
| |
| currentDirItems=itemList[:] |
| print(currentDirItems) |
| |
| |
| separator = "_" |
| |
| not_meet = [] |
| for item in itemList: |
| |
| |
| first_char=item[0] |
| is_english_alpha =first_char.islower() or first_char.isupper() |
| is_valid=is_english_alpha or first_char.isdigit() |
| |
| |
| if not is_valid: |
| not_meet.append(item) |
| |
| else: |
| ... |
| itemList = not_meet |
| print("@itemList",itemList) |
| |
| for index, item in enumerate(itemList): |
| |
| |
| |
| new_prefix = str(index).zfill(2)+separator |
| new_name = new_prefix+item |
| |
| |
| |
| originName = item |
| preview_variations = f'😎newName:{new_name}<-😁originName:{originName}' |
| |
| |
| while( (new_name in currentDirItems)): |
| print(f'already exist the new_name:{new_name}🤣') |
| new_prefix = str(index+1).zfill(2)+separator |
| new_name = new_prefix+item |
| |
| |
| print(preview_variations) |
| os.rename(originName, new_name) |
| |
| |
| def create_files(n:int=2,path:str="."): |
| """ 这是一个创建测试文件的函数,用以检测脚本的主要逻辑是否可以大致的正确工作 """ |
| path="test_rename_dir" |
| if(not os.path.exists(path)): |
| os.mkdir(path) |
| os.chdir("test_rename_dir") |
| |
| mode="w+" |
| for i in range(n): |
| with open(f"{i}.txt", mode) as f: |
| f.write("hello world") |
| with open("中文开头文件",mode) as f: |
| f.write("中文开头文件") |
| with open("(_ascii文件", mode) as f: |
| f.write("(_ascii文件") |
| if __name__ == "__main__": |
| |
| print('😎------------------------------') |
| |
| rename_prefix() |
python 识别英文字母(a-z,A-Z)
| |
| |
| eng_alpha="a" |
| chinese_alpha="中" |
| |
| |
| def is_eng_alpha(char): |
| return char.islower() or char.isupper() |
| |
| |
| def is_eng_alpha2(char): |
| if char>="a" and char<='z' or char>="A" and char<='Z': |
| print("char is english alpha") |
| return True |
| else: |
| print("char is not english alpha") |
| return False |
| if __name__=="__main__": |
| is_eng_alpha2(eng_alpha) |
| is_eng_alpha2(chinese_alpha) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了