How to use glob() to find files recursively?

前端 未结 28 1753
天涯浪人
天涯浪人 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:24

    Just made this.. it will print files and directory in hierarchical way

    But I didn't used fnmatch or walk

    #!/usr/bin/python
    
    import os,glob,sys
    
    def dirlist(path, c = 1):
    
            for i in glob.glob(os.path.join(path, "*")):
                    if os.path.isfile(i):
                            filepath, filename = os.path.split(i)
                            print '----' *c + filename
    
                    elif os.path.isdir(i):
                            dirname = os.path.basename(i)
                            print '----' *c + dirname
                            c+=1
                            dirlist(i,c)
                            c-=1
    
    
    path = os.path.normpath(sys.argv[1])
    print(os.path.basename(path))
    dirlist(path)
    

提交回复
热议问题