[ python-Bugs-1580472 ] glob.glob("c:\\[ ]\*) doesn't work

SourceForge.net noreply at sourceforge.net
Sun Oct 29 23:58:24 CET 2006


Bugs item #1580472, was opened at 2006-10-19 13:44
Message generated for change (Comment added) made by koblaid
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1580472&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Closed
Resolution: Invalid
Priority: 5
Private: No
Submitted By: Koblaid (koblaid)
Assigned to: Nobody/Anonymous (nobody)
Summary: glob.glob("c:\\[ ]\*) doesn't work

Initial Comment:
OS: Windows 2000 Service Pack 4
Python 2.5

glob.glob() doesn't work in directories named 
"[ ]" (with a blank in it). Another example is a 
directory named "A - [Aa-Am]"

Example:
#########################
C:\>md []
C:\>md "[ ]"
C:\>copy anyfile.txt []
        1 Datei(en) kopiert.
C:\>copy anyfile.txt "[ ]"
        1 Datei(en) kopiert.

C:\>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC 
v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for 
more information.
>>> import glob
>>> glob.glob ("c:\\[]\*")
['c:\\[]\\anyfile.txt']
>>> glob.glob ("c:\\[ ]\*")
[]
#########################

The second glob should have resulted the same as the 
first glob since I copied the same file to both 
directories.
I may be wrong because I'm new to python. But I've 
tested it a couple of times, and I think it have to be 
a bug of python or a bug of windows.

Greets, Koblaid

----------------------------------------------------------------------

>Comment By: Koblaid (koblaid)
Date: 2006-10-29 23:58

Message:
Logged In: YES 
user_id=1624709

Thanks for your answers. Although I'm pretty new at Python, but I disagree with you.

I see, it's not a bug. And folders like "[ ]" aren't very common.
But if you scan your filesystem recursively using glob, you will lose all folders named like 
"[ ]":
>>def scanDir(path):
>>  elements = glob.glob(directoryPath + "\\*")
>>  for currentElement in elements:
>>	if os.path.isfile(currentElement):
>>      print currentElement
>>    else:
>>      scanDir(currentElement)

Even if these folders are very rare, the damage could be great. You lose files without 
recognizing it.
A programmer assums that a language works correctly in all cases. So I think this should be 
changed.

One easy solution is to add a second optional boolean parameter for glob, which have to be 
true, if you want to use regular expressions.
But this and other similar solutions fail, if you try to use glob recursively, as the example 
above shows.
A solution that would work with my example could be that glob returns the paths and puts every 
"[" and "]" (and other affected charecters) in brackets, as potten recommended. On the other 
hand, the result is unhandily if you don't want to use it for glob again.
So I don't no a nice solution. Maybe you have better ideas...

Thanks, Koblaid

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-10-27 16:01

Message:
Logged In: YES 
user_id=849994

Not a bug, as Peter said.

----------------------------------------------------------------------

Comment By: Peter Otten (potten)
Date: 2006-10-27 14:32

Message:
Logged In: YES 
user_id=703365

Not a bug. "[abc]" matches exactly one character which may be "a", "b" or "c". 
Therefore "[ ]" matches one space character. If you want a literal "[", put it in brackets, 
e. g. glob.glob("C:\\[[] ]\\*").  
 
--- 
 
By the way, do you think this problem is common enough to warrant the addition of a 
fnmatch.escape() function? I have something like this in mind: 
 
>>> import re 
>>> r = re.compile("(%s)" % "|".join(re.escape(c) for c in "*?[")) 
>>> def escape(s): 
...     return r.sub(r"[\1]", s) 
... 
>>> escape("c:\\[a-z]\\*") 
'c:\\[[]a-z]\\[*]' 
 

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2006-10-27 08:14

Message:
Logged In: YES 
user_id=341410

This is a known issue with the fnmatch module (what glob
uses under the covers).  According to the documentation of
the translate method that converts patterns into regular
expressions... "There is no way to quote meta-characters." 
The fact that "[]" works but "[ ]" doesn't work is a
convenient bug, for those who want to use "[]".

If you can come up with some similar but non-ambiguous
syntax to update the fnmatch module, I'm sure it would be
considered, but as-is, I can't see this as a "bug" per-se.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1580472&group_id=5470


More information about the Python-bugs-list mailing list