How to traverse through the files in a directory?

前端 未结 9 1747
离开以前
离开以前 2020-11-30 11:34

I have a directory logfiles. I want to process each file inside this directory using a Python script.

for file in directory:
      # do something


        
相关标签:
9条回答
  • 2020-11-30 12:01

    In Python 2, you can try something like:

    import os.path
    
    def print_it(x, dir_name, files):
        print dir_name
        print files
    
    os.path.walk(your_dir, print_it, 0)
    

    Note: the 3rd argument of os.path.walk is whatever you want. You'll get it as the 1st arg of the callback.

    In Python 3 os.path.walk has been removed; use os.walk instead. Instead of taking a callback, you just pass it a directory and it yields (dirpath, dirnames, filenames) triples. So a rough equivalent of the above becomes

    import os
    
    for dirpath, dirnames, filenames in os.walk(your_dir):
        print dirpath
        print dirnames
        print filenames
    
    0 讨论(0)
  • 2020-11-30 12:01
    import os
    # location of directory you want to scan
    loc = '/home/sahil/Documents'
    # global dictonary element used to store all results
    global k1 
    k1 = {}
    
    # scan function recursively scans through all the diretories in loc and return a dictonary
    def scan(element,loc):
    
        le = len(element)
    
        for i in range(le):   
            try:
    
                second_list = os.listdir(loc+'/'+element[i])
                temp = loc+'/'+element[i]
                print "....."
                print "Directory %s " %(temp)
                print " "
                print second_list
                k1[temp] = second_list
                scan(second_list,temp)
    
            except OSError:
                pass
    
        return k1 # return the dictonary element    
    
    
    # initial steps
    try:
        initial_list = os.listdir(loc)
        print initial_list
    except OSError:
        print "error"
    
    
    k =scan(initial_list,loc)
    print " ..................................................................................."
    print k
    

    I made this code as a directory scanner to make a playlist feature for my audio player and it will recursively scan all the sub directories present in directory.

    0 讨论(0)
  • 2020-11-30 12:01
    import os
    rootDir = '.'
    for dirName, subdirList, fileList in os.walk(rootDir):
        print('Found directory: %s' % dirName)
        for fname in fileList:
            print('\t%s' % fname)
        # Remove the first entry in the list of sub-directories
        # if there are any sub-directories present
        if len(subdirList) > 0:
            del subdirList[0]
    

    0 讨论(0)
提交回复
热议问题