best way to check if a file exists?

Michael S msherman77 at yahoo.com
Tue Oct 31 16:56:55 EST 2006


If you want to open the file for writing just open it
with append mode. open(¨filename", "a"). If it
doesn't exist - it'll create it, and if it does it'll
start appending to the file

For reading - os.path.exists("filename"). Or (doesn't
make much sense, but still) try to open it for reading
and python will throw an IOException which you can
catch and handle.

Michael

--- John Salerno <johnjsal at NOSPAMgmail.com> wrote:

> What is the best way to check if a file already
> exists in the current 
> directory? I saw os.path.isfile(), but I'm not sure
> if that does more 
> than what I need.
> 
> I just want to check if a file of a certain name
> exists before the user 
> creates a new file of that name.
> 
> Thanks.
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list