简体   繁体   中英

python3 glob.glob regex only getting first match

Having a bit of a weird issue, as this code seems to work perfectly well on my friends mac, but is not at all working on my ubuntu 16.04.

Through my python, im running the following

filenames = glob.glob(opts['-I'])

which is attempting to match to a set of 32 text files with the format TEXT/text01.txt

My initial regex was the following

python -I TEXT/text??

Which returned zero files.

I have also tried

python -I TEXT/text*

Which seems to only be returning text01.txt. Is there a corresponding regex that can get all of the text files instead of just the first one, and is there any reason why this is working on a mac but not ubuntu?

the problem when you're calling

python -I TEXT/text*

is that TEXT/text* is expanded by the shell. So these are the exact args passed to python:

-I TEXT/text01.txt TEXT/text02.txt (and other matching files)

argparser assigns text01.txt to the -I option and other arguments are ignored (check positional arguments to find them). glob.glob returns the exact filename it recieves (note that text?? doesn't match text01.txt because you're missing the extension, or make it text??.txt )

You need to quote your wildcard (not regex)

python -I "TEXT/text*"

or escape the wildcard:

python -I TEXT/text\*

or use a more prehistoric command line like windows CMD where the wildcards are passed literally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM