Reputation: 5136
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('.')))
Upvotes: 4
Views: 14353
Reputation: 11929
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)
Upvotes: 6
Reputation: 15887
You're telling the OS to move test-dir
, not its contents. It would normally replace the target (.
in this case) but that target obviously isn't empty, so the implicit rmdir
fails. Even if it weren't empty, it's likely impossible to remove or replace the .
name.
The shell *
is a glob, which would expand to each thing within test-dir
, which you could move individually; however, you'd want to transfer their name to the target directory, i.e. test-dir/foobar
to ./foobar
. os.path.basename
can help you extract that portion.
Upvotes: 0