Vinayak Kolagi Vinayak Kolagi - 1 year ago 84
Python Question

Move child folder contents to parent folder in python

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


I want the contents of 'slave' folder to be moved to "slave1" (parent folder). Once moved,
'slave' folder should be deleted. shutil.move seems to be not helping.

Please let me know how to do it ?

Answer Source

Example using the os and shutil modules:

from os.path import join
from os import listdir, rmdir
from shutil import move

root = 'dstfolder/slave1'
for filename in listdir(join(root, 'slave')):
    move(join(root, 'slave', filename), join(root, filename))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download