python批量重命名乱码文件

找到一些文件,想把乱码去掉,光留下数字和下划线

代码:

# -*- coding: utf-8 -*-
#!/usr/bin/python
##author: jxbm
##reference  http://stackoverflow.com/questions/1052225/convert-python-filenames-to-unicode
import sys
import os
import re
def UsePrompt():
    print 'batchrename newfilename'
    sys.exit()

def BatchRename():
    r=re.compile(r'[^0-9-_]')
    os.chdir(r'.')
    fileList=os.listdir(u'.') ##the u is the key!!!!!!!!!!!!!!!!!
    
    for fi in fileList:
##        fi.encode('ascii').decode('utf-8')
##        fi.decode('gbk').encode('utf8')
##        fi.decode('utf-8').encode('gbk')
##        fi.decode('utf-8')
        print (fi)
        if(fi.endswith('rar')):
            str=r.sub('',fi).replace('-','_')
##            print(str)
            print(str+'.rar')
            os.rename(fi,str+'.rar')
        else:
                print (fi)
BatchRename()

注意 因为乱码文件名是utf-8编码的,代码中的u是关键!

上张效果图……

cpmpare

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值