How to get all of the immediate subdirectories in Python

前端 未结 15 1675
旧时难觅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:37

    import glob
    import os
    
    def child_dirs(path):
         cd = os.getcwd()        # save the current working directory
         os.chdir(path)          # change directory 
         dirs = glob.glob("*/")  # get all the subdirectories
         os.chdir(cd)            # change directory to the script original location
         return dirs
    

    The child_dirs function takes a path a directory and returns a list of the immediate subdirectories in it.

    dir
     |
      -- dir_1
      -- dir_2
    
    child_dirs('dir') -> ['dir_1', 'dir_2']
    

提交回复
热议问题