How to copy folder structure under another directory?

后端 未结 3 1357
日久生厌
日久生厌 2021-02-15 23:51

I have some questions related to copying a folder structure. In fact, I need to do a conversion of pdf files to text files. Hence I have such a folder structure for the place wh

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-16 00:31

    For me the following works fine:

    • Iterate over existing folders

    • Build the structure for the new folders based on existing ones

    • Check, if the new folder structure does not exist
    • If so, create new folder without files

    Code:

    import os
    
    inputpath = 'D:/f/'
    outputpath = 'D:/g/'
    
    for dirpath, dirnames, filenames in os.walk(inputpath):
        structure = os.path.join(outputpath, dirpath[len(inputpath):])
        if not os.path.isdir(structure):
            os.mkdir(structure)
        else:
            print("Folder does already exits!")
    

    Documentation:

    • os.walk
    • os.mkdir
    • os.path.isdir

提交回复
热议问题