Prevent expansion of wildcards in non-quoted python script argument when running in UNIX environment

2017-07-07T21:26:43

I have a python script that I'd like to supply with an argument (usually) containing wildcards, referring to a series of files that I'd like to do stuff with. Example here:

#!/usr/bin/env python

import argparse
import glob 

parser = argparse.ArgumentParser()
parser.add_argument('-i', action="store", dest="i")
results = parser.parse_args()
print 'argument i is: ', results.i
list_of_matched_files = glob.glob(results.i)

In this case, everything works great if the user adds quotes to the passed argument like so:

./test_script.py -i "foo*.txt"

...but often times the users forget to add quotes to the argument and are stumped when the list only contains the first match because UNIX already expanded the list and argparse only then gets the first list element.

Is there a way (within the script) to prevent UNIX from expanding the list before passing it to python? Or maybe even just to test if the argument doesn't contain quotes and then warn the user?

Copyright License:
Author:「HotDogCannon」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/44971986/prevent-expansion-of-wildcards-in-non-quoted-python-script-argument-when-running

About “Prevent expansion of wildcards in non-quoted python script argument when running in UNIX environment” questions

I have a python script that I'd like to supply with an argument (usually) containing wildcards, referring to a series of files that I'd like to do stuff with. Example here: #!/usr/bin/env python ...
I am currently developing an application meant to be an improved environment variables editor for Windows. I have used both System.Environment.GetEnvironmentVariables() and Registry to get them. H...
How do Unix filename wildcards work from Python? A given directory contains only subdirectories, in each of which there is (among others) one file whose name ends with a known string, say _ext. The
My ruby shell scripts specify the ruby interpreter in the first line of the script as: #!/Users/me/.rvm/rubies/ruby-1.9.3-p194/bin/ruby The problem is that when I upgrade to a new ruby version, I...
I'm running a Perl script (MOSS, Measure of Software Similarity) that uploads either a file or a folder of files to a server, but I'm running into some weird behaviors. This is supposed to work:
I run my python script after launching Python environment in bash. Then I want to examine a variable defined in my script. But I cannot. I wonder how I can run the script in Python while still bein...
I've got a jq command that works when running directly from the shell or from within a shell script, but when I try to add variable expansion, I get jq errors for unexpected format or invalid chara...
I have a UNIX script which is used to initialize the environment and properties of the project. It uses python. I had to refer the path. This will be used as a common file and used across multiple
I'm running Mac OS X (10.14.4) and I'm attempting to follow YouTube tutorial; https://www.youtube.com/watch?v=bDaxeg4HKQY; Image Detection with YOLO v2 Process Video in Python + openCV In a UNIX
I have a python script, which takes a keyboard entry and enters it into a csv file. There will be a number of Raspberries which perform the same task and I want to look at a management process, to ...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.