[Tutor] Why doesn't getopt() handle this character '^' ?

Abel Daniel abli@freemail.hu
Mon Mar 3 10:19:01 2003


Tony Cappellini (tony@tcapp.com) wrote:
> When I pass these arguments to my module
> 
> 2 ^ 2  as in (python mymodule.py 2 ^ 2)
> 
> This is what is displayed
> 
> Args=
> ['2', '2']
> 
> This is the code in my module
> 
> import sys, operator
> opts, args = getopt( sys.argv[1:], "")
> print"\nArgs= "
> print args
> 
> getopts() works for other operators
> as in 2 * 2
> 
> Args=
> ['2', '*', '2']

I could exactly reproduce the effect you describe. My guess is that the
shell you type 'python mymodule.py 2 ^ 2' into mangles the ^ character.
Using the bash shell, 'python mymodule.py 2 ^ 2' works as expected (the
^ character gets passed), but 'python mymodule.py 2 * 2' does something
wierd: the shell replaces the * character with the list of files in the
current directory. This means that the program never sees the *
character. This subsitution makes things like 'ls *.txt' work.

You should try quoting the parameters, like this:
python mymodule.py "2 ^ 2"
This tells the shell to pass those unchanged to you program. (So for me 
python mymodule.py "2 * 2"
prints ['2', '*', '2'] and not the filelist.)

Hope this helps
abli