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

Leazen leazen at uol.com.ar
Tue Nov 5 10:03:30 EST 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The other answers are probably better but if you like the C way you can use:

access(path, mode)
Check read/write/execute permissions for this process or existence of
file path. mode should be F_OK to test the existence of path, or it can
be the inclusive OR of one or more of R_OK, W_OK, and X_OK to test
permissions. Return 1 if access is allowed, 0 if not. See the Unix man
page access(2) for more information. Availability: Unix, Windows.

F_OK
Value to pass as the mode parameter of access() to test the existence of
path.

Just like you could in C.

Leazen



Christopher R. Culver wrote:
> 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
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE9x91ohcKLjUu3XMkRAuXkAJ41GInFHMQ5lN9DvkjNvSxayOR9gACfUsp7
y/OY8LtErAuVNyKZzTAM4Ys=
=n0h5
-----END PGP SIGNATURE-----







More information about the Python-list mailing list