FAQ
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

Search Discussions

  • Javier Collado at Dec 30, 2010 at 8:52 am
    Hello,

    2010/12/30 <smainklh at free.fr>:
    How can i do the same thing (wildcard in a directory name) in python please ?
    You can get the contents of a directory with os.listdir and filter
    with fnmatch.fnmatch more or less as in the example from the
    documentation:
    ---------------------
    import fnmatch
    import os

    for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
    print file
    ---------------------

    Regards,
    Javier
  • Peter Otten at Dec 30, 2010 at 9:01 am

    smainklh at free.fr 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
  • Cameron Simpson at Dec 30, 2010 at 9:04 am

    On 30Dec2010 09:36, smainklh at free.fr wrote:
    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 ]
    [...]

    Check out the glob module:
    http://docs.python.org/library/glob.html#module-glob

    Use it to do the glob, then os.path.isfile with a path constructed from
    the result:

    http://docs.python.org/library/os.path.html#os.path.isfile

    Cheers,
    --
    Cameron Simpson <cs at zip.com.au> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    Any company large enough to have a research lab
    is large enough not to listen to it. - Alan Kay
  • Smainklh at Dec 30, 2010 at 1:35 pm
    Hi Cameron,

    Ok, i'll try that :)

    Thanks
    Smaine

    Selon Cameron Simpson <cs at zip.com.au>:
    On 30Dec2010 09:36, smainklh at free.fr wrote:
    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 ]
    [...]

    Check out the glob module:
    http://docs.python.org/library/glob.html#module-glob

    Use it to do the glob, then os.path.isfile with a path constructed from
    the result:

    http://docs.python.org/library/os.path.html#os.path.isfile

    Cheers,
    --
    Cameron Simpson <cs at zip.com.au> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    Any company large enough to have a research lab
    is large enough not to listen to it. - Alan Kay

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 30, '10 at 8:36a
activeDec 30, '10 at 1:35p
posts5
users4
websitepython.org

People

Translate

site design / logo © 2021 Grokbase