Python处理MLDonkey 下载中文文件乱码问题

王朝other·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

python文件内容如下:

#

-*- coding: cp936 -*-

import

os ,string ,urllib ,operator

#

文件替换目录路径

sdir='/usr/local/mldonkey/incoming/'

#

数字标记

sNum='0123456789'

#

遍历目录做文件名转换

def

convert():

filenames=os.listdir(sdir)

for filename in filenames :

if filename != convertName(filename)

:

print filename + '

>>> ' + convertName(filename)

os.rename(sdir + filename

,sdir + convertName(filename))

#

转换一个文件名

def

convertName(s) :

location = 0

ret = ""

while True :

if location + 8 <= len(s) :

subStr = s[location:location + 8]

if check(subStr) :

ret += "%" +

hex((int)(subStr[1:4]))[2:4] + "%" + hex((int)(subStr[5:8]))[2:4]

location = location + 8

else :

ret += s[location :location +

1]

location = location + 1

else :

ret += s[location:]

break

return urllib.unquote(ret)

#

检查一个字符串是否需要做转换

def

check(s):

if len(s) != 8 :

return False

if s[0] != '_' or s[4] != '_' :

return False

if (s[1] in sNum) and (s[2] in sNum) and

(s[3] in sNum) and (s[5] in sNum) and (s[6] in sNum) and (s[7] in sNum) :

return True

return False

#

执行文件名转换

convert()

要运行此脚步,需要Python2.4,在命令行输入:

python mldonkey.py

运行即可。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航