os.access with wildcards

Mike Meyer mwm at mired.org
Fri Oct 7 13:44:07 EDT 2005


"mike" <no.mail.here.at.here at gmail.com> writes:

> Test for the existence of one or more matches of the wildcard
> expression.
>
> For example:
>
>     Are there any files that begin with 2005?
>
> This doesn't work (wish it did):
>      os.access('2005*',os.F_OK)

I would considering it suprising if it worked. os.access is for
testing access permissions of a single file. There's no reasonn for it
to do glob expansion on the name, as there can be only one such
name. Maybe if it took multiple names, but then you have to decide if
you're going to return the "or" or the "and" of the results of the
test. Or - more - likely - provide an optional argument to specify the
operation.

This is getting ugly. Best just leave it as it is.

> However, these work arounds do the job:
>     glob.glob('2005*')==[]

> [Not very readable.]

Yup. A standard idiom in most languages for testing for a list to be
empty is to check the length of the list:

      len(glob.glob('2005*')) == 0

Which I'd say is more readable. In Python, an empty list is false, so
you can skip the test completely, and just ask for the boolean
negation of the list:

      not glob.glob('2005*')

I'd probably do it that way.

    <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list