Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > os.path.isfile and wildcard for directory name

Reply
Thread Tools

os.path.isfile and wildcard for directory name

 
 
smainklh@free.fr
Guest
Posts: n/a
 
      12-30-2010


Hi everyone,

I'm just beginning to learn python language and i'm trying to do something and i
can't figure it out.

I want to test if a file exists but my path contain a directory name that
differs from a server to another.
In shell i would have done something like that :

#!/bin/bash

mypath=/dire*/directory02/
myfile=filename

myfile=toto

if [ -f $mypath/$myfile ]
then
echo "File $file exists"
fi


How can i do the same thing (wildcard in a directory name) in python please ?

Thanks for your help !

Smaine
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-30-2010
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I'm just beginning to learn python language and i'm trying to do something
> and i can't figure it out.
>
> I want to test if a file exists but my path contain a directory name that
> differs from a server to another.
> In shell i would have done something like that :
>
> #!/bin/bash
>
> mypath=/dire*/directory02/
> myfile=filename
>
> myfile=toto
>
> if [ -f $mypath/$myfile ]
> then
> echo "File $file exists"
> fi
>
>
> How can i do the same thing (wildcard in a directory name) in python
> please ?


Given

$ mkdir yadda{1..10}
$ touch yadda{5,7}/alpha
$ mkdir yadda{2,4}/alpha

You can get a list of candidates with

>>> import glob
>>> candidates = glob.glob("yadda*/alpha")
>>> candidates

['yadda5/alpha', 'yadda2/alpha', 'yadda4/alpha', 'yadda7/alpha']

and then use isfile() to find the actual files:

>>> import os
>>> [f for f in candidates if os.path.isfile(f)]

['yadda5/alpha', 'yadda7/alpha']

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wildcard String Comparisons: Set Pattern to a Wildcard Source chaoticcranium@gmail.com Python 7 10-05-2010 09:26 PM
directory wildcard hong zhang Python 2 11-16-2009 09:01 PM
Getting a list of files in a directory using a wildcard Michael McGarry C Programming 18 11-05-2005 05:27 PM
wildcard extension ASP.Net httphandler gives 401.3 ACL permission denied error on a directory Ken Yee ASP .Net 2 10-07-2005 04:03 PM
wildcard extension ASP.Net httphandler gives 401.3 ACL permission denied error on a directory Ken Yee ASP .Net Security 2 10-07-2005 04:03 PM



Advertisments