python批量修改文件夹下的图片名称(3种方法) |
您所在的位置:网站首页 › python批量修改文件名加前缀 › python批量修改文件夹下的图片名称(3种方法) |
python批量修改文件夹下的图片名称 本人下载了逐日1KM LST数据,但是里面有部分文件夹下的数据命名和其他的数据命名不一致,所以需要批量命名。 添加自己的文件路径和修改自己要的图片名称,本方法是直接在原文件路径中修改图片名称,若不想覆盖之前的图片名,请先备份。 import os import re #coding=utf-8 """批量修改文件夹的图片名""" def ReFigName(dirPath): ####param dirPath: 文件夹路径 # 对目录下的文件进行遍历 for file in os.listdir(dirPath): print(file) # 判断是否是文件 if os.path.isfile(os.path.join(dirPath, file)) == True: newfile = file[0:7] + "N" + ".tif"##在此处修改自己想要的图片名即可 # 重命名 os.rename(os.path.join(dirPath, file), os.path.join(dirPath, newfile)) print("***修改成功***") if __name__ == '__main__': dirPath = r"...\myt\myt2"###添加自己的文件路径即可 ReFigName(dirPath)
方法2:用python的shutil的rename。 import os import re import shutil #coding=utf-8 """批量修改文件夹的图片名""" def ReFileName(dirPath): ####param dirPath: 文件夹路径 # 对目录下的文件进行遍历 for file in os.listdir(dirPath): print(file) # 判断是否是文件 if os.path.isfile(os.path.join(dirPath, file)) == True: newfile = file[0:7] + "N" + ".tif" # 重命名 os.rename(os.path.join(dirPath, file), os.path.join(dirPath, newfile)) print("***搞定了哈!!!***")方法3:用python的shutil的copy函数,在复制的时候修改。这样不用担心覆盖原来的文件。 import os import re import shutil #coding=utf-8 """批量修改文件夹的图片名""" def ReFileName(dirPath,outPath): ####param dirPath: 文件夹路径 # 对目录下的文件进行遍历 for file in os.listdir(dirPath): print(file) # 判断是否是文件 if os.path.isfile(os.path.join(dirPath, file)) == True: newfile = file[0:7] + "N" + ".tif" # 重命名 shutil.copy(os.path.join(dirPath, file),os.path.join(outPath, newfile)) print("***搞定了哈!!!***") |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |