Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Version of glob for that Supports Windows Wildcards?
Is there a Python module that works like glob except that it works properly on Windows?

Here's the problem. Windows recognizes "?" and "*" as wildcards. Windows does NOT recognize "[" or "]" as special characters, whereas glob uses them to denote character classes. Many of my scripts are set to operate on either a file or a file pattern. Technically all file names are patterns. They are just patterns that match at most one file. So a loop like

for file in glob.glob(pattern):

works just fine under Windows as long as an actual pattern with a wildcard is given. It fails when

1. a non-wildcard filename is given and
2. that filename contains "[" or "]"

If my folder contains the files

Awards Day [1994].mp4
Betula Lake [1983].mp4
Piano Recital [1994].mp4

and my pattern is "Awards Day [1994].mp4" then the loop does not find the file. While glob provides the glob.escape method, it also escapes "?" and "*" thus making the Windows wildcards ineffective for pattern matching.

One "solution" which I expect someone to propose is to rename all my files to avoid the use of "[" or "]". This is not a solution. I should be allowed to use any valid character that Windows allows.
I don't have a solution for you that I think is elegant, but you can can do something silly like
pattern.replace("[", "[[").replace("]", "]]")
to do the escaping you need.
Write own regex,glob work many general cases(not for many different corner cases) with a set of predefined pattern eg *.txt is in regex .*\\.txt\\Z(?ms)

Can use eg os.scandir for looping over files,with own regex that match wanted files.
import os
import re

match = re.compile(r'.*\[\d+\].*').match
for fn in os.scandir('.'):
    if match(
Awards Day [1994].mp4 Betula Lake [1983].mp4 Piano Recital [1994].mp4
If i just comment out line 6,the read all files in folder.
Awards Day [1994].mp4 Betula Lake [1983].mp4 filename.csv foo.txt Piano Recital [1994].mp4
noisefloor likes this post
As it turns out
pattern.replace("[", "[[").replace("]", "]]")
doesn't work, however
pattern.replace("[", "[[]")
does the trick. Thanks for the suggestion. Silly? Yes. Inelegant? Also yes, but it's short and I can easily tag it with a "#kludge" end-of-line comment so that if something more elegant comes along I can easily do a find/replace.
Just use glob.escape, then you don't have to think about regex or escaping in source code.

In [1]: import glob                                                                                                             

In [2]: '*' + glob.escape('[') + '???' + glob.escape(']') + '*.mp3'                                                             
Out[2]: '*[[]???]*.mp3'
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Yeah. Tried that. Unfortunately, glob.escape also escapes "*" which means I can no longer use Windows/DOS wildcards in my file names.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [split] Using this Python version of text2pdf jonsnow1221 1 106 May-07-2019, 07:48 PM
Last Post: micseydel
  Using this Python version of text2pdf Pedroski55 1 159 Apr-15-2019, 08:12 AM
Last Post: Pedroski55
  can't install package web in version python 3.7 a21250450 1 147 Mar-29-2019, 11:28 AM
Last Post: Larz60+
  Hello Im looking for a compiled window version of 3.4.x ... SlowMotion 2 175 Feb-12-2019, 08:13 PM
Last Post: SlowMotion
  Python Version on Mac OSX Sparkstorm 5 271 Feb-06-2019, 10:32 AM
Last Post: Sparkstorm
  numpynot associted with correct python version ErnestTBass 17 750 Jan-16-2019, 02:47 PM
Last Post: ErnestTBass
  Python version 3 that seems to be ignored by the system on a Raspberry Pi Discret68 3 352 Dec-28-2018, 06:42 PM
Last Post: Gribouillis
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 245 Dec-19-2018, 05:41 PM
Last Post: CodeWolf
  How to comapare version UUIDs in python? swamyjrs 3 277 Dec-19-2018, 08:57 AM
Last Post: Gribouillis
  Python version compatability saisankalpj 5 376 Nov-29-2018, 06:12 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)