[python-win32] pure python way to open a file with write deny for others

Robin Becker robin at reportlab.com
Thu Mar 5 05:42:52 EST 2020


I want to be able to read a windows file which is being periodically written by another process. I created a small 
extension which does the following

	if(!PyArg_ParseTuple(args, "sss", &fn, &mode, &deny)) return NULL;
	if(strcmp(deny, "") == 0)		share = _SH_DENYNO; /*allow all*/
	else if (strcmp(deny, "r") == 0)	share = _SH_DENYRD; /*deny read*/
	else if (strcmp(deny, "w") == 0)	share = _SH_DENYWR; /*deny write*/
	else if (strcmp(deny, "rw") == 0)	share = _SH_DENYRW; /*deny read/write*/
	f = _fsopen(fn, mode, share);
	if (!f){
		PyErr_SetFromErrno(PyExc_Exception);
		r = NULL;
		ERROR_EXIT();
		}
	else{
		r = PyFile_FromFile(f, fn, mode, fclose);
		if(!r) ERROR_EXIT();
		}

that seems to work, but I wonder if there's an easier pure python way to do this. Looking at the docs I see O_EXCL, but 
the _SH_DENY flags seem to be absent.
-- 
Robin Becker


More information about the python-win32 mailing list