Skip to content

Inaccurate and misleading document for shutil.move in case src/target are on the same filesystem #109503

Open

Description

@fangli

Documentation

The document/docstring for shutil.move states:

If the destination is on the current filesystem, then :func:os.rename is used.
Otherwise, src is copied to dst using copy_function and then removed.

which seems to be incorrect. The actual behavior shows below:

  • If src and target are on the same filesystem, os.rename is preferred, which is an atomic operation.
  • But, if os.rename fails due to some reason (e.g. permission denied), then shutil.move will silently fallback to nonatomic copy+remove.

This fallback behavior is crucial in case atomic operation matters. It creates false statement that an operation must be atomic, while in fact is non-atomic in some cases.

This behavior can be found in source code here.

For example

Given all following dirs and files on the SAME filesystem

// 600: rw_ ___ ___
// 700: rwx ___ ___

userA:groupA 700  /dirSrc
userA:groupA 600  /dirSrc/fileSrc

userB:groupB 700  /dirTarget
userA:groupA 600  /dirTarget/fileTarget

When the userA calls:

# Run by userA
shutil.move('/dirSrc/fileSrc', '/dirTarget/fileTarget')
# Move succeeded

Based on current doc, user expects that the move must be atomic since both files are on the same filesystem. But in fact, this operation will fall back to copy+del silently, which is not atomic.

Linked PRs

Activity

  1. added a commit that references this issue on Sep 17, 2023
  2. changed the title [-]Inaccurate and misleading documentation for `shutil.move` in case src/target on the same filesystem[/-] [+]Inaccurate and misleading document for `shutil.move` in case src/target on the same filesystem[/+] on Sep 17, 2023
  3. changed the title [-]Inaccurate and misleading document for `shutil.move` in case src/target on the same filesystem[/-] [+]Inaccurate and misleading document for `shutil.move` in case src/target are on the same filesystem[/+] on Sep 17, 2023
  4. added 6 commits that reference this issue on Sep 17, 2023
  5. added a commit that references this issue on Oct 2, 2023
  6. added a commit that references this issue on May 30, 2026
  7. added 3 commits that reference this issue on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions