[Tutor] os.rename vs. shutil.move

Steven D'Aprano steve at pearwood.info
Sun Apr 17 01:15:11 CEST 2011


Bill Allen wrote:
> What are the particular advantages or disadvantages concerning using either
> os.rename or shutil.move to rename a file.   I have tried both and for
> simple renaming they seem equivalent.

Consider non-simple renaming.

At the interactive interpreter, call:

help(os.rename)

and then

help(shutil.move)

to be enlightened.

And read the Fine Manual:

http://docs.python.org/library/shutil.html

which conveniently now links directly to the source code, so you can see 
for yourself all the extra work shutil.move does to cover cases which 
os.rename may not cover (depending on the OS and/or file system).

http://docs.python.org/library/os.html#os.rename

os.rename is a thin wrapper around your file system's rename/mv command, 
with the same limitations as it has. shutil.move attempts to do extra 
work to overcome such limitations.



-- 
Steven



More information about the Tutor mailing list