Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving files under python

I'm confused with file moving under python. Under windows commandline, if i have directory c:\a and a directory c:\b, i can do

move c:\a c:\b

which moves a to b result is directory structure c:\b\a

If I try this with os.rename or shutil.move:

os.rename("c:/a", "c:/b")

I get

WindowsError: [Error 17] Cannot create a file when that file already exists

If I move a single file under c:\a, it works.

In python how do i move a directory to another existing directory?

like image 999
Ash Avatar asked Jul 09 '09 09:07

Ash


People also ask

Can you move files with Python?

Python provides functionality to move files or directories from one location to another location. This can be achieved using shutil. move() function from shutil module.

What is move () in Python?

The Python shutil. move() method moves a file to another location on your computer. This method is part of the shutil model, which you must import before using this method.


1 Answers

os.rename("c:/a", "c:/b/a") 

is equivalent to

move c:\a c:\b

under windows commandline

like image 75
sunqiang Avatar answered Sep 22 '22 16:09

sunqiang