os.walk without hidden folders

后端 未结 3 1812
悲哀的现实
悲哀的现实 2020-11-30 06:17

I need to list all files with the containing directory path inside a folder. I tried to use os.walk, which obviously would be the perfect solution.

Howe

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 06:52

    My use-case was similar to that of OP, except I wanted to return a count of the total number of sub-directories inside a certain folder. In my case I wanted to omit any sub-directories named .git (as well as any folders that may be nested inside these .git folders).

    In Python 3.6.7, I found that the accepted answer's approach didn't work -- it counted all .git folder and their sub-folders. Here's what did work for me:

    num_local_subdir = 0
    for root, dirs, files in os.walk(local_folder_path):
        if '.git' in dirs:
            dirs.remove('.git')
        num_local_subdir += (len(dirs))
    

提交回复
热议问题