Browse files and subfolders in Python

前端 未结 5 1817
有刺的猬
有刺的猬 2020-11-28 03:29

I\'d like to browse through the current folder and all its subfolders and get all the files with .htm|.html extensions. I have found out that it is possible to find out whet

5条回答
  •  执念已碎
    2020-11-28 04:04

    I had a similar thing to work on, and this is how I did it.

    import os
    
    rootdir = os.getcwd()
    
    for subdir, dirs, files in os.walk(rootdir):
        for file in files:
            #print os.path.join(subdir, file)
            filepath = subdir + os.sep + file
    
            if filepath.endswith(".html"):
                print (filepath)
    

    Hope this helps.

提交回复
热议问题