1. 程式人生 > >python 重命名本地文件

python 重命名本地文件

ace pytho from os.path IT light pri name spl


# -*- coding:utf-8 -*-
import os
from xpinyin import Pinyin
test = Pinyin()
def get_name():
    path = r"D:\pic\sx\pinyin_tb"
    for filename in os.listdir(r"D:\pic\sx\pinyin_tb"):              #listdir的參數是文件夾的路徑
        print ( filename)
        old_dir = os.path.join(path,filename)
        new_name =  ‘‘.join(test.get_pinyin(filename).split(‘-‘)).replace(".png",".jpg")
        print(new_name)
        new_dir = os.path.join(‘D:\pic\sx\pinyin_tb‘,new_name)
        print(new_dir)
        os.rename(old_dir,new_dir)

if __name__ == ‘__main__‘:
   get_name()

  

python 重命名本地文件