shutil.move(src, dst) is what I feel will do the job, however, as per the Python 2 documentation:
shutil.move(src, dst) Recursively move a file or directory (src) to another location (dst).
If the destination is an existing directory, then src is moved inside that directory. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.
This is a little bit different than my case as below:
Before the move: https://snag.gy/JfbE6D.jpg
shutil.move(staging_folder, final_folder)
After the move: https://snag.gy/GTfjNb.jpg
This is not what I want, I want all the content in the staging folder be moved over to under folder "final" , I don't need "staging" folder itself.
You can use shutil.copytree() to move all contents in staging_folder to final_folder without moving the staging_folder . Pass the argument copy_function=shutil.move when calling the function.
For Python 3.8:
shutil.copytree('staging_folder', 'final_folder', copy_function=shutil.move, dirs_exist_ok=True)
For Python 3.7 and below:
beware that the paramater dirs_exist_ok is not supported. The destination final_folder must not already exist as it will be created during moving.
shutil.copytree('staging_folder', 'final_folder', copy_function=shutil.move)
>>> os.listdir('staging_folder')
['file1', 'file2', 'file3']
>>> os.listdir('final_folder')
[]
>>> shutil.copytree('staging_folder', 'final_folder', copy_function=shutil.move, dirs_exist_ok=True)
'final_folder'
>>> os.listdir('staging_folder')
[]
>>> os.listdir('final_folder')
['file1', 'file2', 'file3']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With