classification
Title: os.stat() WindowsError 13 when file in use
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: loewis Nosy List: abkhd, dcroberts, georg.brandl, loewis
Priority: normal Keywords:

Created on 2007-03-23 02:31 by dcroberts, last changed 2007-04-30 01:54 by abkhd. This issue is now closed.

Files
File name Uploaded Description Edit
stat.diff loewis, 2007-03-30 06:29
Messages (7)
msg31627 - (view) Author: d roberts (dcroberts) Date: 2007-03-23 02:31
Using 2.5 Sept 19, 2006 on Windows XP, 32-bit.

os.stat() on an "in use" file (i.e., open by another process) erroneously returns WindowsError [Error 13]. The file stats should be available on an open file, even if you cannot read/write the file itself.

Python 2.4 correctly returns the stats on the file.

----------------
CORRECT in 2.4:
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
>>> import os
>>> os.stat('c:\\hiberfil.sys')
(33206, 0L, 2, 1, 0, 0, 804311040L, 1173962381, 1173962381, 1069302780)

-----------------
ERROR in 2.5:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
>>> import os
>>> os.stat("c:\\hiberfil.sys")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
WindowsError: [Error 13] The process cannot access the file because it is being
used by another process: 'c:\\hiberfil.sys'
msg31628 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-23 10:15
May this be related to rev. 42230 - "Drop C library for stat/fstat on Windows." ?
msg31629 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-03-23 10:24
It certainly is; Python is likely opening the file in the wrong mode now. I'll investigate, although contributions would be welcome.
msg31630 - (view) Author: d roberts (dcroberts) Date: 2007-03-23 17:49
Taking your hint, I just looked at the code in 42230 which uses the Win32 GetFileAttributesEx() function. This function does indeed return an error for an in-use file. Not sure if this is a "feature" or "bug" in the underlying Windows function. I did try FindFirstFile(), which is able to return info on an in-use file. Not sure if this is a reasonable alternative to GFAE(), but wanted to make folks aware of the possibility. It's probably slower, since it can handle wild-cards, and returns a handle for subsequent calls to FindNextFile(). The handle must be closed with FindClose(). I'm not sure what underlying mechanism FindFirstFile() uses, but I'm assuming it accesses the "directory" to get its information, rather than trying to access the file itself. FWIW.
msg31631 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-03-30 06:29
Attached is a patch that makes it fall back to FindFirstFile if GetFileAttributesEx fail with ERROR_SHARING_VIOLATION.
File Added: stat.diff
msg31632 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-04-04 18:31
I have now committed this patch as r54685 and r54686.
msg31633 - (view) Author: A.B., Khalid (abkhd) Date: 2007-04-30 01:54
Please see http://www.python.org/sf/1709112 for comment and patch.
History
Date User Action Args
2007-03-23 02:31:29dcrobertscreate