How to get all of the immediate subdirectories in Python

前端 未结 15 1678
旧时难觅i
旧时难觅i 2020-11-29 17:09

I\'m trying to write a simple Python script that will copy a index.tpl to index.html in all of the subdirectories (with a few exceptions).

I\'m getting bogged down

15条回答
  •  生来不讨喜
    2020-11-29 17:45

    import pathlib
    
    
    def list_dir(dir):
        path = pathlib.Path(dir)
        dir = []
        try:
            for item in path.iterdir():
                if item.is_dir():
                    dir.append(item)
            return dir
        except FileNotFoundError:
            print('Invalid directory')
    

提交回复
热议问题