How to use glob() to find files recursively?

前端 未结 28 1760
天涯浪人
天涯浪人 2020-11-21 22:54

This is what I have:

glob(os.path.join(\'src\',\'*.c\'))

but I want to search the subfolders of src. Something like this would work:

<
28条回答
  •  隐瞒了意图╮
    2020-11-21 23:33

    I modified the top answer in this posting.. and recently created this script which will loop through all files in a given directory (searchdir) and the sub-directories under it... and prints filename, rootdir, modified/creation date, and size.

    Hope this helps someone... and they can walk the directory and get fileinfo.

    import time
    import fnmatch
    import os
    
    def fileinfo(file):
        filename = os.path.basename(file)
        rootdir = os.path.dirname(file)
        lastmod = time.ctime(os.path.getmtime(file))
        creation = time.ctime(os.path.getctime(file))
        filesize = os.path.getsize(file)
    
        print "%s**\t%s\t%s\t%s\t%s" % (rootdir, filename, lastmod, creation, filesize)
    
    searchdir = r'D:\Your\Directory\Root'
    matches = []
    
    for root, dirnames, filenames in os.walk(searchdir):
        ##  for filename in fnmatch.filter(filenames, '*.c'):
        for filename in filenames:
            ##      matches.append(os.path.join(root, filename))
            ##print matches
            fileinfo(os.path.join(root, filename))
    

提交回复
热议问题