wumingxiaoyao

python查找文件相同的和包含汉字的

#!/usr/bin/env python
# Version = 3.5.2
import os
import time

d_path = \'/data/media\'
log_file = \'result.log\'


def con_chinese(s):
    """
    包含汉字的返回TRUE
    :param s: 要判断的字符串
    :return: 返回值
    """
    for c in s:
        if \'\u4e00\' <= c <= \'\u9fa5\':
            return True
    return False


def con_zad(s):
    """
    只包含字母数字的返回TRUE
    :param s: 要判断的字符串
    :return: ret
    """
    ret = True
    for c in s.replace(\'.\', \'\'):
        if 48 <= ord(c) < 128:
            pass
        else:
            ret = False
            break
    return ret

s_time = time.time()
with open(log_file, \'w\') as l:
    for pathname, dirnames, filenames in os.walk(d_path):
        # 查找目录名称相同的或有汉字的
        for dir in dirnames:
            if con_chinese(dir):
                try:
                    l.write(\'1--\' + os.path.join(pathname, dir) + \'\n\')
                except Exception as e:
                    l.write(\'5--\' + pathname + str(e) + \'\n\')
            else:
                for i in dirnames:
                    if con_zad(i) and con_zad(dir):
                        if i.lower() == dir.lower() and i != dir:
                            try:
                                l.write(\'3--\' + os.path.join(pathname, dir) + \'\n\')
                                break
                            except Exception as e:
                                l.write(\'5--\' + pathname + \'----\' + str(e) + \'\n\')
        # 查找文件名称相同的
        for file in filenames:
            for x in filenames:
                if con_zad(x) and con_zad(file):
                    if x.lower() == file.lower() and x != file:
                        try:
                            l.write(\'4--\' + os.path.join(pathname, x) + \'\n\')
                            break
                        except Exception as e:
                            l.write(\'5--\' + pathname + \'----\' + str(e) + \'\n\')

    f_time = time.time()
    l.write(\'用时:\' + str(f_time-s_time) + \'秒\n\')

  

发表于 2018-01-04 17:23  无名小妖  阅读(1379)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章: