8

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?

1
  • do you want to move all contents of the directory to another directory or move the directory itself? Commented Aug 5, 2011 at 14:37

6 Answers 6

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

is equivalent to

move c:\a c:\b

under windows commandline

Sign up to request clarification or add additional context in comments.

Comments

7

You can try using the Shutil module.

1 Comment

Specifically shutil.move(srcFile, destFile)
1

os.rename("c:/a/", "c:/b"/) --> Changes the name of folder a in folder b

os.rename("c:/a/", "c:/b/a") --> Put folder b into folder a

Comments

0

When i need many file system operations I prefer using 'path' module:
http://pypi.python.org/pypi/path.py/2.2

It's quite a good and lightweight wrapper around built-in 'os.path' module.

Also code:

last_part = os.path.split(src)[1]

is a bit strange, cause there is special function for this:

last_part = os.path.basename(src)

1 Comment

Excellent point -- forgot about basename. I don't like how there's more than one way to do that. ;-)
-1

You will need to state the full path it's being moved to:

src = 'C:\a'
dst_dir = 'C:\b'
last_part = os.path.split(src)[1]
os.rename(src, os.path.join(dst_dir, last_part))

Actually, it looks like shutil.move will do what you want by looking at its documentation:

If the destination is a directory or a symlink to a directory, the source is moved inside the directory.

(And its source.)

5 Comments

Unfortuneatly this will fail if the files are located on different volumes.
@wuub: What makes you say that? The docs say there's quirky behavior in some UNIX filesystems, but the OP is talking about Windows.
Hmm, this is straightforward for a single directory, but moving a big directory structure to another folder is really inconvenient this way. Quite strange that python doesn't support this.
It does -- shutil.move you can use via: import shutil; shutil.move(src, dst)
@cdleary: You're right, I advice against os.rename because of past problems I had with it while coding a toy FUSE filesystem, shutil on the other hand works great no matter what the OS is.
-1

Using Twisted's FilePath:

from twisted.python.filepath import FilePath
FilePath("c:/a").moveTo(FilePath("c:/b/a"))

or, more generally:

from twisted.python.filepath import FilePath
def moveToExistingDir(fileOrDir, existingDir):
    fileOrDir.moveTo(existingDir.child(fileOrDir.basename()))
moveToExistingDir(FilePath("c:/a"), FilePath("c:/b"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.