chenkehao 发表于 2017-5-5 09:21:39

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

python文件内容如下:
#-*- coding: cp936 -*-
importos ,string ,urllib ,operator

#文件替换目录路径
sdir='/usr/local/mldonkey/incoming/'

#数字标记
sNum='0123456789'

#遍历目录做文件名转换
defconvert():
filenames=os.listdir(sdir)
for filename in filenames :
if filename != convertName(filename):
print filename + '>>> ' + convertName(filename)
os.rename(sdir + filename,sdir + convertName(filename))

#转换一个文件名
defconvertName(s) :
location = 0
ret = ""
while True :
if location + 8 <= len(s) :
subStr = s
if check(subStr) :
ret += "%" +hex((int)(subStr)) + "%" + hex((int)(subStr))
location = location + 8
else :
ret += s
location = location + 1
else :
ret += s
break
return urllib.unquote(ret)

#检查一个字符串是否需要做转换
defcheck(s):
if len(s) != 8 :
return False

if s != '_' or s != '_' :
return False

if (s in sNum) and (s in sNum) and(s in sNum) and (s in sNum) and (s in sNum) and (s in sNum) :
return True
return False

#执行文件名转换
convert()
要运行此脚步,需要Python2.4,在命令行输入:
python mldonkey.py
运行即可。
页: [1]
查看完整版本: Python处理MLDonkey 下载中文文件乱码问题