This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Albert.Zeyer, Seth.Troisi, eryksun, hitbox, joernheissler, nedbat, nneonneo, sanjioh, serhiy.storchaka
Date 2020-01-14.16:58:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
> I afraid that removing a file while the file descriptor is open 
> may not work on Windows. Seems this case is not well tested.

os.remove will succeed on a file that's opened with O_TEMPORARY, which shares delete access (i.e. FILE_SHARE_DELETE).

With classic Windows delete semantics, deleting a file sets the delete disposition on the filesystem's underlying file/link control block (FCB/LCB). The filesystem doesn't unlink the file from the directory until all File object references have been finalized. (A File handle refers to a kernel File object, which refers to an FCB/LCB in a filesystem. An FCB/LCB can be referenced by multiple File objects, such as from multiple CreateFileW calls, and a File object can be referenced by multiple handles, such as via inheritance or DuplicateHandle.) The deleted file remains accessible to existing File objects, and a File object with delete access can even be used to clear the delete disposition (i.e. undelete the file) via SetFileInformationByHandle: FileDispositionInfo. The file remains linked and visible in the parent directory, but no new access is allowed while its delete disposition is set. The parent directory cannot itself be deleted until the file is unlinked.

In Windows 10 1903, DeleteFileW has also been updated to use POSIX semantics if the filesystem supports it, which includes NTFS on the system drive, where temp files are usually created. With POSIX semantics, the file is unlinked as soon as DeleteFileW closes its File handle. All existing File objects can continue to access the file even though it's no longer linked in the directory. This includes being able to call GetFinalPathNameByHandleW, which, at least for NTFS, will show that the file has been moved to the special system directory "\$Extend\$Deleted" and renamed according to its file ID. As is usual for a deleted file, no new access is allowed, i.e. we cannot reopen a file in the "$Deleted" directory. Another change with POSIX semantics is that the delete is final. Existing File objects that have delete access are not allowed to clear the delete disposition (i.e. undelete the file).
Date User Action Args
2021-03-28 03:36:06eryksununlinkissue39318 messages
2020-01-14 16:58:51eryksunsetrecipients: + eryksun, nneonneo, nedbat, Seth.Troisi, Albert.Zeyer, serhiy.storchaka, sanjioh, joernheissler, hitbox
2020-01-14 16:58:51eryksunsetmessageid: <>
2020-01-14 16:58:51eryksunlinkissue39318 messages
2020-01-14 16:58:50eryksuncreate