How to get all of the immediate subdirectories in Python

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

    import os, os.path
    

    To get (full-path) immediate sub-directories in a directory:

    def SubDirPath (d):
        return filter(os.path.isdir, [os.path.join(d,f) for f in os.listdir(d)])
    

    To get the latest (newest) sub-directory:

    def LatestDirectory (d):
        return max(SubDirPath(d), key=os.path.getmtime)
    

提交回复
热议问题