This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: glob.glob("c:\\[ ]\*) doesn't work
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, josiahcarlson, koblaid, peter.otten
Priority: normal Keywords:

Created on 2006-10-19 11:44 by koblaid, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg30299 - (view) Author: Koblaid (koblaid) Date: 2006-10-19 11:44
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
msg30300 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2006-10-27 06:14
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.
msg30301 - (view) Author: Peter Otten (peter.otten) * Date: 2006-10-27 12:32
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]\\[*]' 
 
msg30302 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-10-27 14:01
Logged In: YES 
user_id=849994

Not a bug, as Peter said.
msg30303 - (view) Author: Koblaid (koblaid) Date: 2006-10-29 22:58
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
History
Date User Action Args
2022-04-11 14:56:20adminsetgithub: 44143
2006-10-19 11:44:08koblaidcreate