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) + '
')
                except Exception as e:
                    l.write('5--' + pathname + str(e) + '
')
            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) + '
')
                                break
                            except Exception as e:
                                l.write('5--' + pathname + '----' + str(e) + '
')
        # 查找文件名称相同的
        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) + '
')
                            break
                        except Exception as e:
                            l.write('5--' + pathname + '----' + str(e) + '
')

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

  

原文地址:https://www.cnblogs.com/wumingxiaoyao/p/8194522.html