python将文件名变为拼音

python 源码巴士 225浏览 0评论
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from pypinyin import pinyin, lazy_pinyin, Style

def scan_files(directory,prefix=None,postfix=None):
    files_list=[]
    
    for root, sub_dirs, files in os.walk(directory):
        for special_file in files:
            if postfix:
                if special_file.endswith(postfix):
                    files_list.append(os.path.join(root,special_file))
            elif prefix:
                if special_file.startswith(prefix):
                    files_list.append(os.path.join(root,special_file))
            else:
                files_list.append(os.path.join(root,special_file))
                          
    return files_list

path = "/mnt/d/wamp64/www/tree/wap/images/newtheme"
files = scan_files(path, None, 'png')
# print(files)
for file in files:
	filename = file
	file_pinyin = lazy_pinyin(filename)
	filename_pinyin = ''.join(file_pinyin)
	try:
		os.rename(str(filename), str(filename_pinyin))
	except Exception as e:
		print(e)
	

转载请注明:源码巴士 » python将文件名变为拼音

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址