How does Python handle probing to see if a file already exists?
Terry Reedy
tjreedy at udel.edu
Tue Nov 5 12:33:37 EST 2002
More information about the Python-list mailing list
Tue Nov 5 12:33:37 EST 2002
- Previous message (by thread): How does Python handle probing to see if a file already exists?
- Next message (by thread): How does Python handle probing to see if a file already exists?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
<sismex01 at hebmex.com> wrote in message news:mailman.1036508171.27010.python-list at python.org... > > From: Christopher R. Culver [mailto:Kricxjo.neniuspamajxo at yahoo.com] > > However, translating that directly into Python doesn't work because > > if Python can't find a file to open, it doesn't just return 0, it > > gives an error and exits. How can I write this functionality in Python > > so that it returns 0 and continues, or does Python have a totally > > different way of handling this problem than C? > Others have talked about the goodness of using try/except, I'll > say to use os.access(), which is precisely for what you're asking, > if you can read or write a file. > > -- check if writeable -- > > import os > if os.access("some-filename", os.W_OK): > # neat, I can write to the file. But only IF nothing happens to the file between the before-leap look and the leap itself. If another process deletes the file, changes permissions, or opens it for writing in between, then the OP is back where he started -- getting an uncaught, program-crashing exception. Or opening could fail for lack of OS open-file slots, buffers, whatever or writing could fail for lack of disk space. Whether or not this is a serious concern depends on the system and enironment the program runs in. TJR
- Previous message (by thread): How does Python handle probing to see if a file already exists?
- Next message (by thread): How does Python handle probing to see if a file already exists?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list