[python-win32] pure python way to open a file with write deny for others
Eryk Sun
eryksun at gmail.com
Thu Mar 5 11:04:49 EST 2020
On 3/5/20, Robin Becker <robin at reportlab.com> wrote:
> I want to be able to read a windows file which is being periodically written
> by another process.
I'm having difficulty reconciling this sentence with the subject line.
If you want to open a file for reading that's already open for
writing, then the open has to share write access, not deny it (where
to "deny" access means to not share access) [1]. That's not an issue
since Python shares read and write access when opening a file. (Note
that the share mode applies to all opens. It is not related to
processes, so whether it's another process or the current process
that's writing to the file is irrelevant to the problem.)
I wouldn't use FILE streams in Python 3. I'd pass an `opener` to
Python `open` that uses ctypes or PyWin32 to get a handle via
`CreateFileW`, and then msvcrt.open_osfhandle to wrap the handle with
a C file descriptor. The opener function signature is opener(filename,
flags) -> fd.
[1]: https://docs.microsoft.com/en-us/windows/win32/fileio/creating-and-opening-files
More information about the python-win32
mailing list