Glob returning an empty list when passed a variable

Discussion in 'Python' started by Neil Webster, Feb 9, 2007.

  1. Neil Webster

    Neil Webster Guest

    Hi,

    I was wondering whether anybody could help me out.

    I have a program, for part of it I am trying to pass a variable to a
    glob function, this returns an empty list. The strange thing is when
    I hard code in the variable the glob section works.

    Does anybody have any ideas as why it is not working?

    The section of code that is not working is:

    # The variable to be passed to the glob function
    area_name_string = '"*% s*"' % (Area_name)

    os.chdir(Input)

    filename = glob.glob(area_name_string)

    Thanks in advance

    Neil
    Neil Webster, Feb 9, 2007
    #1
    1. Advertising

  2. Neil Webster <> wrote:

    > area_name_string = '"*% s*"' % (Area_name)
    > os.chdir(Input)
    > filename = glob.glob(area_name_string)


    Too many quotation marks.

    >>> Area_name='Foo'
    >>> '"*% s*"' % (Area_name)

    '"*Foo*"'

    Unless there are files with funny names containing '"' you will not get a
    match.

    cu
    Philipp

    --
    Dr. Philipp Pagel Tel. +49-8161-71 2131
    Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186
    Technical University of Munich
    http://mips.gsf.de/staff/pagel
    Philipp Pagel, Feb 9, 2007
    #2
    1. Advertising

  3. Neil Webster

    Steve Holden Guest

    Neil Webster wrote:
    > Hi,
    >
    > I was wondering whether anybody could help me out.
    >
    > I have a program, for part of it I am trying to pass a variable to a
    > glob function, this returns an empty list. The strange thing is when
    > I hard code in the variable the glob section works.
    >
    > Does anybody have any ideas as why it is not working?
    >
    > The section of code that is not working is:
    >
    > # The variable to be passed to the glob function
    > area_name_string = '"*% s*"' % (Area_name)
    >
    > os.chdir(Input)
    >
    > filename = glob.glob(area_name_string)
    >
    > Thanks in advance


    Because you are trying to match filenames that have a double-quote
    character at the start and end? Try

    area_name_string = '*% s*' % (Area_name)

    Interesting, I never realised until now that you can have spaces between
    the percent sign and th format effector.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
    Steve Holden, Feb 9, 2007
    #3
  4. Neil Webster

    Neil Webster Guest

    On 9 Feb, 14:15, Steve Holden <> wrote:
    > Neil Webster wrote:
    > > Hi,

    >
    > > I was wondering whether anybody could help me out.

    >
    > > I have a program, for part of it I am trying to pass a variable to a
    > > glob function, this returns an empty list. The strange thing is when
    > > I hard code in the variable the glob section works.

    >
    > > Does anybody have any ideas as why it is not working?

    >
    > > The section of code that is not working is:

    >
    > > # The variable to be passed to the glob function
    > > area_name_string = '"*% s*"' % (Area_name)

    >
    > > os.chdir(Input)

    >
    > > filename = glob.glob(area_name_string)

    >
    > > Thanks in advance

    >
    > Because you are trying to match filenames that have a double-quote
    > character at the start and end? Try
    >
    > area_name_string = '*% s*' % (Area_name)
    >
    > Interesting, I never realised until now that you can have spaces between
    > the percent sign and th format effector.
    >
    > regards
    > Steve
    > --
    > Steve Holden +44 150 684 7255 +1 800 494 3119
    > Holden Web LLC/Ltd http://www.holdenweb.com
    > Skype: holdenweb http://del.icio.us/steve.holden
    > Blog of Note: http://holdenweb.blogspot.com
    > See you at PyCon? http://us.pycon.org/TX2007- Hide quoted text -
    >
    > - Show quoted text -


    Steve and Philipp,

    Thanks very much for the promptness of the reply and providing the
    answer.

    Steve, it appears to work so I left it, should it not be possible?

    Regards

    Neil
    Neil Webster, Feb 9, 2007
    #4
  5. On Fri, 09 Feb 2007 14:15:51 +0000, Steve Holden wrote:

    > area_name_string = '*% s*' % (Area_name)
    >
    > Interesting, I never realised until now that you can have spaces between
    > the percent sign and th format effector.


    Space is one of the flags. From the docs:


    The conversion flag characters are:

    Flag Meaning
    # The value conversion will use the ``alternate form'' (where defined
    below).
    0 The conversion will be zero padded for numeric values.
    - The converted value is left adjusted (overrides the "0" conversion if
    both are given).
    (a space) A blank should be left before a positive number (or empty
    string) produced by a signed conversion.
    + A sign character ("+" or "-") will precede the conversion (overrides a
    "space" flag).


    http://docs.python.org/lib/typesseq-strings.html



    --
    Steven.
    Steven D'Aprano, Feb 9, 2007
    #5
  6. Neil Webster

    Steve Holden Guest

    Steven D'Aprano wrote:
    > On Fri, 09 Feb 2007 14:15:51 +0000, Steve Holden wrote:
    >
    >> area_name_string = '*% s*' % (Area_name)
    >>
    >> Interesting, I never realised until now that you can have spaces between
    >> the percent sign and th format effector.

    >
    > Space is one of the flags. From the docs:
    >
    >
    > The conversion flag characters are:
    >
    > Flag Meaning
    > # The value conversion will use the ``alternate form'' (where defined
    > below).
    > 0 The conversion will be zero padded for numeric values.
    > - The converted value is left adjusted (overrides the "0" conversion if
    > both are given).
    > (a space) A blank should be left before a positive number (or empty
    > string) produced by a signed conversion.
    > + A sign character ("+" or "-") will precede the conversion (overrides a
    > "space" flag).
    >
    >
    > http://docs.python.org/lib/typesseq-strings.html
    >
    >>> "% s" % 'banana'

    'banana'
    >>> "% s" % 1

    '1'
    >>> "% s" % -1

    '-1'
    >>>


    Since it appears non-operative in the case of strings I'd be tempted to
    leave it out, though my original post was triggered by my surprise that
    I'd not seen the feature before. There are no limits to my ignorance :)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
    Steve Holden, Feb 10, 2007
    #6
  7. Neil Webster

    Guest

    On Feb 10, 3:32 pm, Steve Holden <> wrote:
    >
    > >>> "% s" % 'banana'

    > 'banana'
    > >>> "% s" % 1

    > '1'
    > >>> "% s" % -1

    > '-1'
    > >>>

    >


    With some number:

    In [2]: "% 3s" % 'a'
    Out[2]: ' a'

    Hieu
    , Feb 10, 2007
    #7
  8. In <>,
    wrote:

    > With some number:
    >
    > In [2]: "% 3s" % 'a'
    > Out[2]: ' a'


    The space still doesn't have any effect here:

    In [66]: "%3s" % 'a'
    Out[66]: ' a'

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Feb 10, 2007
    #8
  9. Neil Webster

    MRAB Guest

    On Feb 10, 8:32 am, Steve Holden <> wrote:
    > Steven D'Aprano wrote:
    > > On Fri, 09 Feb 2007 14:15:51 +0000, Steve Holden wrote:

    >
    > >> area_name_string = '*% s*' % (Area_name)

    >
    > >> Interesting, I never realised until now that you can have spaces between
    > >> the percent sign and th format effector.

    >
    > > Space is one of the flags. From the docs:

    >
    > > The conversion flag characters are:

    >
    > > Flag Meaning
    > > # The value conversion will use the ``alternate form'' (where defined
    > > below).
    > > 0 The conversion will be zero padded for numeric values.
    > > - The converted value is left adjusted (overrides the "0" conversion if
    > > both are given).
    > > (a space) A blank should be left before a positive number (or empty
    > > string) produced by a signed conversion.
    > > + A sign character ("+" or "-") will precede the conversion (overrides a
    > > "space" flag).

    >
    > >http://docs.python.org/lib/typesseq-strings.html

    >
    > >>> "% s" % 'banana'

    > 'banana'
    > >>> "% s" % 1

    > '1'
    > >>> "% s" % -1

    > '-1'
    > >>>

    >

    [snip]
    I've just tried it and it works for the "d" format but not the "s"
    format:

    >>> "%d" % 1

    '1'
    >>> "%+d" % 1

    '+1'
    >>> "% d" % 1

    ' 1'
    MRAB, Feb 10, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    723
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    355
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    344
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    392
    Neil Hodgson
    Aug 2, 2004
  5. qanda
    Replies:
    1
    Views:
    109
    Tassilo v. Parseval
    Sep 15, 2003
Loading...

Share This Page