-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
[easy doc] Document os.rename() behavior on Windows when src and dst are on different filesystems #72542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, I am just migrating my code from python 2.7.12 to 3.5.2 In python 2.7.12: In python 3.5.2 I get for the same operation: My question:
|
On Python 3 on Windows, os.rename() is implemented as MoveFileExW() with flags=0. The doc says: "When moving a file, the destination can be on a different file system or volume. If the destination is on another drive, you must set the MOVEFILE_COPY_ALLOWED flag in dwFlags." I guess that the portable fix is to try rename() or fall back on copy(src, dst) + delete(src). -- On Python 2 on Windows, os.rename() is implemented as MoveFileW(). It seems like this function behaves as MoveFileEx() called with MOVEFILE_COPY_ALLOWED: "A new file may be on a different file system or drive." -- Should we add a flag to os.rename() to allow copy, to have a portable API? |
Ah, on Linux rename() also fails if src and dst are on two different filesystems: OSError: [Errno 18] Invalid cross-device link. By the way, the Python document doesn't say anything about operation on two different filesystems :-/ |
3.3 added os.replace, which on Windows entailed a switch from calling MoveFile to MoveFileEx in order to specify the MOVEFILE_REPLACE_EXISTING flag. However, not passing the MOVEFILE_COPY_ALLOWED broke compatibility with os.rename on Windows for versions prior to 3.3. I don't know whether or not this was discussed as an intentional breaking change in order to align the behavior with POSIX rename(). The change seems reasonable to me, plus at this point I don't think much can be done other than to add a note to the docs that the behavior changed in 3.3. |
Hi, I tryed os.replace() as replacement for os.rename() too, For now I switched to shutil.move() but I suppose its not |
In scanning over bpo-8828, I see no discussion of the consequences of not using MOVEFILE_COPY_ALLOWED in Antoine's patch, so it appears that this behavior change was unintentional.
Correct, the copy employed by shutil.move for a cross-volume move is not as optimized as what MoveFile uses (basically CopyFile), but unless you're moving a file that's very large it shouldn't matter, and even then without testing I don't know if it's a significant difference relative to the throughput of the disk(s) involved. |
Depending on your point of view, it *can* be seen as an enhancement. See the documentation of MOVEFILE_COPY_ALLOWED: "If the file is Since shutil.move() already implements the copy+delete fallback, I
https://docs.python.org/dev/library/shutil.html#shutil.move already I also suggest to mention in shutil.move() doc that the source can be |
The documentation of os.rename() should mention that on Windows the "operation will fail if src and dst are on different filesystems". For POSIX, instead of "will" it says "may", but the failure is a certainty in Windows since MOVEFILE_COPY_ALLOWED isn't used. |
This issue is now a matter of *documenting* the Windows behavior. Is there any volunteer to propose a PR? |
i have problem with python version 3.9.1 |
T-VEy, this is a documentation-only issue for versions of Python that are under active development for bug fixes and enhancements -- 3.8, 3.9, and 3.10. |
Eryk, can you please go on details on how are they using different strategies to fix bugs but still it's not enough for the time being?Because I know nothing about python and I recognized it in a minute of opening the files.I don't even have it installed on my pc.I don't even have python, not even version 2.7. On Mon, Mar 8, 2021 at 23:48, Eryk Sun<[email protected]> wrote: T-VEy, this is a documentation-only issue for versions of Python that are under active development for bug fixes and enhancements -- 3.8, 3.9, and 3.10. ---------- Python tracker <[email protected]> |
Hi all, This is my first issue so feedback is welcome. Following @vstinner 's suggestions:
And from @eryksun :
Just 2 short lines:
+ The operation will fail if *src* and *dst* are on different filesystems. Use If nothing to change, I will make a PR soon. |
@eryksun The problem seems to be solved. I think we can close the problem. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: