Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python os.rename directory not empty

I'm trying to do mv test-dir/* ./ but in python. I have written the following code but throws OSError: [Errno 66] Directory not empty:

import os    
os.rename(
    os.getcwd() + '/test-dir',
    os.path.abspath(os.path.expanduser('.')))
like image 308
Vaibhav Mule Avatar asked Oct 23 '25 05:10

Vaibhav Mule


1 Answers

You may want to use shutil.move() to iteratively move the files from a directory to another.
For example,

import os
import shutil

from_dir = os.path.join(os.getcwd(),"test-dir")
to_dir = os.path.abspath(os.path.expanduser('.'))

for file in os.listdir(from_dir):
    shutil.move(os.path.join(from_dir, file), to_dir)
like image 144
abc Avatar answered Oct 24 '25 19:10

abc