Python Forum
Version of glob for that Supports Windows Wildcards?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Version of glob for that Supports Windows Wildcards?
#1
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.
Reply
#2
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.
Reply
#3
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.
Example:
import os
import re

match = re.compile(r'.*\[\d+\].*').match
for fn in os.scandir('.'):
    if match(fn.name):
        print(fn.name)
Output:
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.
Output:
Awards Day [1994].mp4 Betula Lake [1983].mp4 filename.csv file_reg.py foo.txt Piano Recital [1994].mp4 read_test.py
Reply
#4
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.
Reply
#5
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'
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Yeah. Tried that. Unfortunately, glob.escape also escapes "*" which means I can no longer use Windows/DOS wildcards in my file names.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to find out from outside Python (in Windows) the current version of Python? pstein 4 680 Oct-04-2023, 10:01 AM
Last Post: snippsat
  Wildcards in a String? pajd 19 2,460 Oct-12-2022, 09:14 AM
Last Post: rob101
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,413 Oct-21-2021, 03:29 AM
Last Post: buran
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,540 Sep-10-2021, 11:54 AM
Last Post: Winfried
  q re glob.iglob iterator and close jimr 2 2,180 Aug-23-2021, 10:14 PM
Last Post: perfringo
  Paho-mqtt fully supports MQTT 5.0 protocol? nitinkothari17 0 1,528 Jan-12-2021, 11:14 AM
Last Post: nitinkothari17
  Last Python (64bit) version for Windows 7? pstein 3 21,687 Nov-15-2020, 02:09 PM
Last Post: jefsummers
  Listing files with glob. MathCommander 9 4,815 Oct-26-2020, 02:04 AM
Last Post: MathCommander
  Windows 10 Python Version DevSrc8 6 4,791 Aug-07-2020, 12:36 PM
Last Post: DevSrc8
  [split] What version of python works on Windows Vista? tit 1 6,978 Oct-11-2018, 10:08 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020