1. 程式人生 > >檔案的批量重新命名

檔案的批量重新命名

import os

class ImageRename():
    def __init__(self):
        self.path = 'E:\Python_os_study\image'

    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)

        i = 0

        for item in filelist:
            if item.endswith('.jpg'):
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path),  format(str(i), '0>10s') + '.jpg')
                os.rename(src, dst)
                print ('converting %s to %s ...' % (src, dst))
                i = i + 1
        print ('total %d to rename & converted %d jpgs' % (total_num, i))


if __name__ == '__main__':
    newname = ImageRename()
    newname.rename()



#獲取絕對路徑
print(os.path.abspath("osStudy"))
#就是獲取當前目錄,並組合成新目錄
filelist = os.listdir("C:\\Users\\frank804\\Desktop\\test")
#os.getcwd() 獲取當前檔案所在路徑
print(os.getcwd())
#路徑組合
#dst = os.path.join(os.path.abspath(self.path),  format(str(i), '0>10s') + '.jpg')
print(os.path.join(os.getcwd(),'data'))
#格式轉換,當數位不夠時用哪個領來填充
print(format(str(11), '0>10s'))