Move child folder contents to parent folder in python

后端 未结 4 887
走了就别回头了
走了就别回头了 2021-01-05 06:27

I have a specific problem in python. Below is my folder structure.

dstfolder/slave1/slave

I want the contents of \'slave\' folder to

4条回答
  •  暖寄归人
    2021-01-05 07:08

    The problem might be with the path you specified in the shutil.move function

    Try this code

    import os
    import shutil
    for r,d,f in os.walk("slave1"):
        for files in f:
            filepath = os.path.join(os.getcwd(),"slave1","slave", files)
            destpath = os.path.join(os.getcwd(),"slave1")
            shutil.copy(filepath,destpath)
    
    shutil.rmtree(os.path.join(os.getcwd(),"slave1","slave"))
    

    Paste it into a .py file in the dstfolder. I.e. slave1 and this file should remain side by side. and then run it. worked for me

提交回复
热议问题