找到一些文件,想把乱码去掉,光留下数字和下划线
代码:
# -*- 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是关键!
上张效果图……