os.access with wildcards

M

mike

i'd like to use

os.access(path,mode)

where path may contain linux style wildcards.
i've failed so far.
my workaround is the bash command.

os.system('[ -e %s ]' % fn )

any suggestions?
 
M

mike

thanks Leif. poor question on my part.

I had been using

glob.glob(path)==[]

and was looking for something more readable, hence

os.system('[ -e %s ]' % path )

but that doesn't seem like a good idiom for crossplatform.

I thought there may either be a way to escape the wildcards, or an
alternative to os.access, or ....

any othr ideas?
 
M

mike

thanks Leif. poor question on my part.

I had been using

glob.glob(path)==[]

and was looking for something more readable, hence

os.system('[ -e %s ]' % path )

but that doesn't seem like a good idiom for crossplatform.

I thought there may either be a way to escape the wildcards, or an
alternative to os.access, or ....

any other ideas?
 
F

Fredrik Lundh

Leif said:
os.access(glob.glob(path), mode)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: access() argument 1 must be string, not list

it's not clear from the OP if he wants

[os.access(file, mode) for file in glob.glob(path))]

os.access(glob.glob(path)[0], mode)

or, most likely, simply

if glob.glob(path):
... some matching file exists ...

</F>
 
M

mike

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)

However, these work arounds do the job:

glob.glob('2005*')==[]

as does this bash command:

os.system('[ -e %s ]' % path )

The 1st is not very readable and the 2nd has portability issues.
 
M

mike

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)

However, these work arounds do the job:

glob.glob('2005*')==[]

as does this bash command:

os.system('[ -e 2005* ]' )

The 1st is not very readable and the 2nd has portability issues.
 
F

Fredrik Lundh

mike said:
Test for the existence of one or more matches of the wildcard
expression.

why are you reposting variations of your question (in duplicates)
instead of reading the replies? that's not a good way to pass the
turing test.

</F>
 
F

Fredrik Lundh

mike said:
dude, you are the sap that wrote "it's not clear".

followed by three possible solutions to the stated problem, one
of which was marked as "most likely".
get a life.

oh, sorry for wasting my time. can I *plonk* you now?

</F>
 
M

mike

No need to apologize for continuing to waste your
time, self.plonk. Get a life, though, and you'll be happier.

As to your question, well, not before you apologize for
thread crapping.

As for your possible solutions, if you consider any
of yours to be "readable", then i have no interest in
coding with you.

And for

if glob.glob(path):

I had already posted that work around.
 
M

Mike Meyer

mike said:
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
 
M

mike

Thanks Mike. Would there be an idiom using "is"?
somethng like

glob.glob('2005*) is not Empty

I have not figured out what to put on the right hand
side of "is"

I guess, for readability, nothing has come up that
seems _great_. One last effort would be to hide
the code behind a method, and use something
readable to name the method.

Thanks also for the link to amaya. The compile is
running now :)
 
D

Dan Sommers

Thanks Mike. Would there be an idiom using "is"?
somethng like
glob.glob('2005*) is not Empty
I have not figured out what to put on the right hand
side of "is"

Don't put anything there:

if glob.glob('2005*'):
print 'there is at least one matching file'
else:
print 'there are no matching files'

Regards,
Dan
 
M

mike

Hi Dan,

It works, it's elegant, and it uses python strengths.

I guess I have to settle the question of who my audience is. That is
who do I want to make it readable for.

All the solutions so far require some python specific knowledge, and
there are some which are horendous even at that. Perhaps less
dependency in this reagrd, the better. Perhaps not.
 
F

Fredrik Lundh

if glob.glob(...): ...
As for your possible solutions, if you consider any
of yours to be "readable", then i have no interest in
coding with you.

I guess, for readability, nothing has come up that
seems _great_.

It works, it's elegant, and it uses python strengths.

cute. I guess this thread shows that python can grow on you.

</F>
 
M

mike

########################
########################
########################
########################
########################

os.path.exists()

########################
########################
########################
########################
########################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,268
Messages
2,571,344
Members
48,019
Latest member
Migration_Expert

Latest Threads

Top