Python about file I/O

Greg Ewing see_reply_address at something.invalid
Sun Sep 22 21:44:42 EDT 2002


Michael Janssen wrote:

> existence-check is:
> os.access(file,os.F_OK)


But it's generally considered preferable to just go
ahead and try to open the file, and catch the exception
if it fails. Some reasons for preferring this approach
include:

   * It will catch all possible reasons for failing
     to open the file, not just lack of existence or
     permissions.

   * It's not prone to something happening in between
     testing and opening which causes the file to
     no longer be openable.

   * It's more efficient -- only one file system
     operation instead of two.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list