Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filenames with square brackets not loading #1757

Closed

Conversation

thomasaarholt
Copy link
Contributor

Fixes #1325. This way I won't rediscover the bug next year.

Copy link
Member

@ericpre ericpre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It breaks the following:

import hyperspy,api as hs
hs.load('*.hspy')

@thomasaarholt
Copy link
Contributor Author

Interesting, and good find. I’m a bit busy at the moment but I’ll see if I can find the time to try and find a solution.

@thomasaarholt
Copy link
Contributor Author

I didn't realise previously that the asterix in ericpre's case referred to a wildcard used with glob. Writing this comment here to remind myself next time I look at it!

@francisco-dlp francisco-dlp added this to the v1.3.3 milestone Jul 11, 2018
@francisco-dlp francisco-dlp modified the milestones: v1.3.3, 1.4.1 Aug 31, 2018
@francisco-dlp francisco-dlp removed this from the 1.4.1 milestone Oct 23, 2018
@tjof2
Copy link
Contributor

tjof2 commented May 9, 2020

@thomasaarholt perhaps try the following instead of glob.escape to keep it working with the star wildcard while escaping the brackets?

import re

def escape_square_brackets(text):
    rep = dict((re.escape(k), v) for k, v in {"[": "[[]", "]": "[]]"}.items())
    pattern = re.compile("|".join(rep.keys()))
    return pattern.sub(lambda m: rep[re.escape(m.group(0))], text)

For example, say I have two files in /home/data/ like this:

/home/data/afile[1x1].txt
/home/data/afile[1x2].txt

Then:

>>> path = "/home/data/afile[*].txt"
>>> glob.glob(path)
[]
>>> glob.glob(escape_square_brackets(path))
['/home/data/afile[1x2].txt', '/home/data/afile[1x1].txt']

Works OK I think @ericpre?

@tjof2
Copy link
Contributor

tjof2 commented May 13, 2020

That said, maybe you'd want to be able to turn the escaping on/off - what if you want to use "[" and "]" for regex in the path too? @francisco-dlp

@tjof2 tjof2 mentioned this pull request May 14, 2020
17 tasks
@tjof2 tjof2 mentioned this pull request Jun 19, 2020
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants