How to get all of the immediate subdirectories in Python

前端 未结 15 1684
旧时难觅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条回答
  •  猫巷女王i
    2020-11-29 17:33

    Why has no one mentioned glob? glob lets you use Unix-style pathname expansion, and is my go to function for almost everything that needs to find more than one path name. It makes it very easy:

    from glob import glob
    paths = glob('*/')
    

    Note that glob will return the directory with the final slash (as unix would) while most path based solutions will omit the final slash.

提交回复
热议问题