Re: Searching for a file

"Peter J. Holzer" <[email protected]>
Newsgroups gmane.comp.python.general
Message-ID <[email protected]>
On 2025-08-05 22:11:57 -0400, Grant Edwards via Python-list wrote:
> On 2025-08-05, Michael Torrie via Python-list <[email protected]> wrote:
> > On 5/24/25 7:19 PM, Chris Angelico via Python-list wrote:
> >> for dir in dirs:
> >>     try: open(dir + "/" + fn).close()
> >>     except FileNotFoundError: pass
> >>     else: break
> >> 
> >> Is this really all that difficult? Not everything has to be in the stdlib.
> >
> > That would modify atime on unix file systems, no?  Of course most modern
> > file systems on SSDs are mounted with noatime, but still.
> 
> Why not call os.stat() instead of open/close?

The OP used os.path.isfile().

Depending on what you want to achieve, each of three methods might be
appropriate. Calling os.stat() just tells you whether the thing exists
(but of course its return value gives you a lot of extra information).
os.path.isfile() tells you whether it's a file (and it is shorter, too).
open() tells you whether the file is readable.

        hjp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | [email protected]         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmiXl3MACgkQ8g5IURL+
KF1+ahAAlOUEDpGjfwjmFkB4DSzMaWRmw85oCCJl7l1L8hnlOhrWbto1EboJJSf8
eFrM3CaUADBm5oIGYtLtkAhL3vTF4PME2o1jClNDeWMVy11Otz9xNIjj9rItNmtV
4thS/QHqmtj5RYEHJMwM+t88swwlGOiq/2Wvbpazb9tcZeibOFzxUh0wnAv9KzOn
x88lOxdZWh0hJe5rU9oB2NWPuPJxvaZ6AOkH4wtZKylODmd+W/uEZBnPnJO5LlSH
xTjs3MrooDl0/j4WfkJqaMy5CUCYW0rIFHnq5Ikr4fA9t+WcIGbIodYc7hOh54Io
pL30z1lsa4c01RifntCa9uDoH0LY4Xz7ruv2t7PUvdXcPYWN254P7RKS+ilBePKN
hMm0N0FNlmtR4qiIqC/DOEWTwvYmherhe0B2FILK3y3FXoyfYrJXWl1b3DSHQrQz
or26bj7ejYAV4f09mhspx3uq0y65TfmkCNoPgm41VqjGXgJFMrMau3p5btVFuqBK
WWmzMPDPQ5momPVSb51wx2D05WhFCkVWbXaenWw763DWKSXzppF+fIamrrAWtkdn
msS29U5ozR0ewXgeTUt5psGLl3f+znPn58V4PClJ/Gl3IjgVjbSya2xs+/IZsf/L
81d2sOu1T+dmP+/5Nk/WFuqF6XJa45jJnAqSMCRWrP9yJGkubM0=
=3FIT
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.