os.path.getsize() on Windows

Duncan Booth duncan.booth at invalid.invalid
Fri Mar 21 07:35:00 EDT 2008


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:

> I think you're confused. Or possibly I'm confused. Or both.

I think it is you, but then I could be wrong.

> It seems to me that you're assuming that the OP has opened the file
> for reading first, and *then* another process comes along and wants to
> open it for writing. That's not how I read his post: he's trying to
> open a file for reading while it is already being written to by
> another process. Asking for exclusive access when reading isn't going
> to make any difference, because the other process has already opened
> the file for writing.

I'm assuming the other process has already opened the file for writing. In 
that case either it has asked for exclusive access so any attempt to open 
it for reading will fail, or it hasn't in which case Python's default 
'open' will succeed but opening it for exclusive access will fail.

Asking for exclusive access when opening will fail if another process 
already has the file open for reading or writing.

> I suppose it is conceivable that the other process might have opened
> the file for non-exclusive writing, assuming that such a thing is even
> possible, but how likely is that?

The usual situation is that the file is opened for writing but permits 
reading while it is being written. Then opening it to read will succeed 
unless you ask for exclusive access.

BTW, I did test the 'CreateFile' code I posted: I opened the file for 
writing in one Python interpreter, just using open('...', 'w') wrote to it, 
and called flush but didn't close it. Then in another interpreter I checked 
that the CreateFile call threw an exception but open('...', 'r') succeeded 
and I was able to read what had been written. After I closed the file in 
the original interpreter the CreateFile call completed successfully.

Try this:
Session 1:
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit 
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('test.txt', 'w')
>>> f.write('Hello')
>>> f.flush()
>>> # go to session 2
...
>>> f.close()
>>> f = open('test.txt', 'r')
>>> # go to session 2
...
>>> f.close()
>>> # go to session 2
...
>>>

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit 
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32file
>>> handle = win32file.CreateFile("test.txt", win32file.GENERIC_READ, 
0,None,win32file.OPEN_ALWAYS,win32file.FILE_ATTRIBUTE_NORMAL,None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pywintypes.error: (32, 'CreateFile', 'The process cannot access the file 
because it is being used by another process.')
>>> f = open('test.txt', 'r')
>>> f.read()
'Hello'
>>> f.close()
>>> # go back to first session
...
>>> handle = win32file.CreateFile("test.txt", win32file.GENERIC_READ, 
0,None,win32file.OPEN_ALWAYS,win32file.FILE_ATTRIBUTE_NORMAL,None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pywintypes.error: (32, 'CreateFile', 'The process cannot access the file 
because it is being used by another process.')
>>> # go back to first session
...
>>> handle = win32file.CreateFile("test.txt", win32file.GENERIC_READ, 
0,None,win32file.OPEN_ALWAYS,win32file.FILE_ATTRIBUTE_NORMAL,None)
>>>

File open for writing in session 1: CreateFile throws an exception, open 
succeeds.
File open for reading in session 1: CreateFile still throws an exception.
File closed in session 1: CreateFile succeeds.



More information about the Python-list mailing list