python recursive search jpg file and print

Recursive file to find out ( 'jpg', 'gif', 'png', 'JPG', 'GIF', 'PNG') at the end of the file and print

bug encountered: a few good clip following photo files are displayed in JPG format uppercase, lowercase was only taken into account, leading to additional files to .JPG files are printed out at the end. . . Positioning cause problems for a long time   

Legacy: compressed files (RAR or zip) Write Processing 

  Ideas: file decompression pip3 install rarfile have to be unrar.exe on the same directory of the current script

Import OS
 #import rarfile 


DEF get_picture (filepath, Layer):
     # traversing filepath all files, including subdirectories 
    prefix = joinStr ( ' - ' , Layer) 
    Layer + =. 1 IF os.path.isdir (filepath):
         Print (prefix , ' folder ' , filepath, " size = " , os.path.getsize (filepath))
     the else :
         Print (prefix, ' documents ' , filepath, " size = " , os.path.getsize (filepath))
         return

    

    files = os.listdir(filepath)
    for fi in files:
        #文件路径
        fi_d = os.path.join(filepath,fi)
        if os.path.isfile(fi_d):
            prefix = joinStr('-', layer)
            if os.path.basename(fi_d).endswith(('jpg', 'gif', 'png','JPG', 'GIF', 'PNG')):
                 Print (prefix, fi_d, " size = " , os.path.getsize (fi_d))
             elif fi_d.endswith (( ' .zip ' , ' .rar ' )):
                     Print (prefix, " found archive: " , fi_d, " size = " , os.path.getsize (fi_d))
                     # RF = rarfile.RarFile (fi_d) be extracting file # 
                    # current_path = fi_d.split ( '.') [0] # 
                    # rf.extractall (current_path) # extract to the current file path 
                    # IF os.path.isdir (): 
                    #    get_picture (current_path) 
                    # rf.close () 
            the else :
                 Print (prefix, ' other document ' , fi_d, " size = " , os.path.getsize (fi_d))
         # determine whether the folder 
        # If a dir, then again call this function, passing in the current directory, recursive processing. 
        elif os.path.isdir (fi_d): 
            get_picture (fi_d, Layer) 
        the else :
             Print ( " you weak burst " )
     # return 

DEF recursorFile (filePath, Layers):
     "" " 
    : param filePath: file object 
    : param layers: the number of layers
    """
    front = joinStr("-", layers)
    print(front, end="")

    print(os.path.basename(filePath))


def joinStr(str, times):
    rs = ""
    for i in range(times):
        rs += str
    return rs

#recursorFile('F:\\照片', 3)
get_picture('F:\\照片', 0)

 

Guess you like

Origin www.cnblogs.com/eosclover/p/11407512.html