Imagine I have a folder (Folder_1
), and inside it there is another folder (Folder_2
) where I placed some songs documents etc.
Is there a way I can remove the second folder but keep all the documents and songs in the upper folder (Folder_1
)?
Imagine I have a folder (Folder_1
), and inside it there is another folder (Folder_2
) where I placed some songs documents etc.
Is there a way I can remove the second folder but keep all the documents and songs in the upper folder (Folder_1
)?
Here is how you can use shutil.move()
to move files from one path to another:
import os
from glob import glob
from shutil import move
f1 = r'C:\Users\User\Desktop\Folder_1'
f2 = r'C:\Users\User\Desktop\Folder_1\Folder_2'
for f in glob(f2+'//*'):
move(f, os.path.join(f1, os.path.basename(f)))
os.rmdir(f2)
You can also leave out the glob
module, and just use os
and shutil
:
from os import listdir, rmdir
from shutil import move
f1 = r'C:\Users\Caitlin\Desktop\Folder_1'
f2 = 'Folder_2'
for f in listdir(f"{f1}/{f2}"):
print(f)
move(f"{f1}/{f2}/{f}", f"{f1}/{f}")
rmdir(f"{f1}/{f2}")
You could try the following:
import os
import shutil
# move everything from folder 2 into folder 1
for i in os.listdir("[Folder_2 path]"):
shutil.move(os.path.join("[Folder_2 path]", i), "[Folder_1 path]")
# remove the now empty folder
os.rmdir("[Folder_2 path]")