How to get all of the immediate subdirectories in Python

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

    Here's one way:

    import os
    import shutil
    
    def copy_over(path, from_name, to_name):
      for path, dirname, fnames in os.walk(path):
        for fname in fnames:
          if fname == from_name:
            shutil.copy(os.path.join(path, from_name), os.path.join(path, to_name))
    
    
    copy_over('.', 'index.tpl', 'index.html')
    

提交回复
热议问题