简体   繁体   中英

Difference between os.replace() and os.rename()?

I want to move a file form one directory to another in linux with python. I wish to achieve a behavior similar to bash mv command.

What is the difference in practice between the two commands

os.replace()
os.rename()

Is it simply that os.rename() will raise an error if file exists in destination while os.replace() will overwrite it?

Also if another secondary difference that I see it that the os.replace() needs a file as a destination not just the directory.

I can find a direct answer anywhere.

On POSIX systems, the rename system call will silently replace the destination file if the user has sufficient permissions. The same is not true on Windows.

os.replace and os.rename are the same function on POSIX systems, but on Windows os.replace will call MoveFileExW with the MOVEFILE_REPLACE_EXISTING flag set to give the same effect as on POSIX systems.

If you want consistent cross-platform behaviour you should consider using os.replace throughout.

os.rename()

os.rename() method in Python is used to rename a file or directory. This method renames a source file/ directory to specified destination file/directory.

os.replace()

os.replace() method in Python is also used to rename the file or directory.

but:

  • If destination is a directory, OSError will be raised.
  • If the destination exists and is a file, it will be replaced without error if the action performing user has permission.
  • This method may fail if the source and destination are on different filesystems

From docs :

os.replace()

Rename the file or directory src to dst. If dst is a directory, OSError will be raised. If dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement).

os.rename()

Rename the file or directory src to dst. If dst exists, the operation will fail with an OSError subclass in a number of cases: On Windows, if dst exists a FileExistsError is always raised. On Unix, if src is a file and dst is a directory or vice-versa, an IsADirectoryError or a NotADirectoryError will be raised respectively. If both are directories and dst is empty, dst will be silently replaced. If dst is a non-empty directory, an OSError is raised. If both are files, dst it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). This function can support specifying src_dir_fd and/or dst_dir_fd to supply paths relative to directory descriptors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM