Glob returning an empty list when passed a variable

N

Neil Webster

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
 
P

Philipp Pagel

Neil Webster said:
area_name_string = '"*% s*"' % (Area_name)
os.chdir(Input)
filename = glob.glob(area_name_string)

Too many quotation marks.
'"*Foo*"'

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

cu
Philipp
 
S

Steve Holden

Neil said:
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
 
N

Neil Webster

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
 
S

Steven D'Aprano

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

Steve Holden

Steven said:
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

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
 
M

MRAB

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

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top