How does Python handle probing to see if a file already exists?

Christopher R. Culver Kricxjo.neniuspamajxo at yahoo.com
Tue Nov 5 05:28:56 EST 2002


Hello all,

I've just begun learning Python after already getting pretty proficient in
C. However, I'm having a problem with a simple copying program. Ideally,
the program would check to see if the destination already exists and
prompt the user. In this this would be as easy as saying:

probe_destination = fopen(/usr/bin/foo, 'r')
if (probe_destination)
{
some code here;
}

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?

Christopher Culver





More information about the Python-list mailing list