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

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
运行即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值