• little exercise 1 python


    比较两个文件夹里的文件
    1.1 读取两个文件夹里的文件 
    files=os.listdir(r'dir\\\\')
    Q1 编码
    encode decode unicode?
    utf8 gbk ?
    1.2 依次获取文件名和文件后缀
    listdir 直接获得文件名,可以直接比较文件名
    1.3 比较
    cmp(name1,name2)
    1.4 输出不一样的 文件
    判断cmp函数返回值
    /****cmp1.py****/
    import os
    f1=os.listdir(r'C:\Users\R21745\Desktop\ENU')
    f2=os.listdir(r'C:\Users\R21745\Desktop\ELL')
    len1=len(f1)
    len2=len(f2)
    n=0
    for i in range(8):
        x=cmp(f1[i],f2[i])
        if x!=0:
            print "f1"+" "*(5+len(f1[i]))+"f2"
            print f1[i]+" "*5+f2[i]
            n+=1
        else:
            pass
        print n
    ----------------
    result:
    f1           f2
    cmp.py     eLCStandStud_student.chm
    1
    f1                             f2
    eLCStandStud_student.chm     ELCStand_teacher.chm
    2
    f1                         f2
    ELCStand_teacher.chm     image026.jpg
    3
    ---------------------------------------------------------------
    rethink
    2.1 怎么随机比较文件(可能文件夹里的文件顺序不一致,默认f1[0]和f2[0]进行比较的)
    2.2如何格式化输出 使结果易读 
    2.3 两文件数量不一致时的处理
    /*****cmp2.py*****/
    print len1,len2
    for i in range(len1):
        if f1[i] in f2:
            pass
        else:
            print "f1: "+f1[i]
    
    for j in range(len2):
        if f2[j] in f1:
            pass
        else:
            print "f2: "+f2[j]
    ----------------------------------------------------------------------
    rethink
    3.1 文件名后缀忽略大小写
    /*****cmp3.py*********/
    print len1,len2
    for i in range(len1):
        s=f1[i].split(".")
        s1=s[0]+"."+s[-1].lower()
        s2=s[0]+"."+s[-1].upper()
        if s1 in f2 or s2 in f2:
            pass
        else:
            print "f1: "+f1[i]
            n+=1
        
    print n
    for j in range(len2):
        x=f2[j].split(".")
        x1=x[0]+"."+x[-1].lower()
        x2=x[0]+"."+x[-1].upper()
        if x1 in f1 or x2 in f1:
            pass
        else:
            print "f2: "+f2[j]
  • 相关阅读:
    如何证明Application Domain的隔离性
    Programming clojure – Multimethods
    Kafka: a Distributed Messaging System for Log Processing
    Practical Clojure Parallel Programming
    The Joy of Clojure – Clojure philosophy(1)
    Columnar Storage
    Programming clojure – Concurrency
    Practical clojure – Macros and Metaprogramming
    Linkedin Databus
    Leiningen
  • 原文地址:https://www.cnblogs.com/20120810bubu/p/3049105.html
Copyright © 2020-2023  润新知