Jun-14-2019, 10:37 PM
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
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.
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
1 |
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.