Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move child folder contents to parent folder in python

Tags:

python

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

dstfolder/slave1/slave

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 ?

like image 263
Vinayak Kolagi Avatar asked Dec 08 '11 09:12

Vinayak Kolagi


1 Answers

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))
rmdir(root)
like image 141
tito Avatar answered Sep 19 '22 19:09

tito