python批量修改图片名称

import os

class BatchRename():

      def rename(self):
          # windows环境
          """
            os.rename() 方法用于命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError。
            语法:rename()方法语法格式如下:
            os.rename(src, dst)
            参数
                src -- 要修改的目录名
                dst -- 修改后的目录名

          :return:
          """
          path="D:\image_file"
          filelist=os.listdir(path)
          total_num = len(filelist)
          i=1
          for item in filelist:
              if item.endswitch('.jpg'):
                  src=os.path.join(os.path.abspath(path),item)
                  dst=os.path.join(os.path.abspath(path),''+str(i)+'.png') #可根据自己需求选择格式
                  # dst=os.path.join(os.path.abspath(path),'00000'+format(str(i))+'.jpg') #可根据自己需求选择格式,自定义图片名字
                  try:
                      os.rename(src,dst) #src:原名称  dst新名称d
                      i+=1
                  except:
                      continue
          print ('total %d to rename & converted %d png'%(total_num,i))

if __name__=='__main__':
    demo = BatchRename()
    demo.rename()
"""
图片进行base64编码转化 一:文件的打开模式
了解过字符编码都知道,文件都是以某一种标准编码成二进制存在硬盘里的,
在文件的默认打开模式下f = open('a.txt','rt',encoding='utf-8')
其中的t表示是以文本模式打开文件,在应用程序给操作系统发送数据请求后,
操作系统在硬盘读取二进制编码,然后返还给应用程序,通过open方法,将编码解码成我们看到的字符;
如果是以b模式打开文件的话,open方法不会对操作系统返回的二进制数据作处理,而是直接打印。

f = open('a.txt','rt',encoding='utf-8')

其中的t表示是以文本模式打开文件,在应用程序给操作系统发送数据请求后,
操作系统在硬盘读取二进制编码,然后返还给应用程序,通过open方法,
将编码解码成我们看到的字符,那么在b模式下打开文件,操作系统返还直接是一串二进制数字

文件的三种打开方式:
只读模式:‘r’
只写方式:'w'
追加模式:'a'
以字节模式打开文件的话,需要注意:
一定要写上‘b’,只能以rb,wb,ab这种形式打开文件,不能省略‘b’。

"""
import base64 with open("D:\image_1.png","rb") as f: base64_data = base64.b64encode(f,read()) file = open("D:\my.txt","wt") file.write(base64_data) file.close() with open("D:\my.txt","r") as f: base64_data = base64.b64decode(f,read()) file = open("D:\image_1.png","wb") #写成图片格式 file.write(base64_data) file.close()

更多的python文件操作:https://www.cnblogs.com/li1992/p/8633417.html 这篇博客写的很详细。

原文地址:https://www.cnblogs.com/1314520xh/p/14258758.html