Python-Ref > User interaction > User input > Passing arguments containing wildcard characters
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Passing arguments containing wildcard characters

As was mentioned in Passing arguments containing whitespace, passing of command-line arguments has some specific problems. The first one in handling of whitespace characters.
The second possible problem arises from the fact that python interpreter gets the command-line arguments from the command-line intepreter after it has done some preliminary work on it. Most importantly the command-line interpreter expands the wildcard characters.
For this reason the program below does not have the "*" character as the single argument, but gets the filenames that match the "*" wildcard instead.
Expand/Shrink
This is an example text in an example file.
It even has
several
lines.
Argumenty příkazové řádky:
python userinp2.1.py *
Zdroj: (userinp2.1.py)
  1   import sys
  2   
  3   print sys.argv
  4   print sys.argv[0]
  5   print sys.argv[1:]
stdout:
['userinp2.1.py', 'example.txt', 'userinp2.1.py']
userinp2.1.py
['example.txt', 'userinp2.1.py']
Doba běhu: 21.0 ms
While this behavior is desirable most of the times, it complicates the use of wildcard characters in command-line input. That could be a problem if one should write a program that will for example search a dictionary for all words matching a certain pattern including wildcards.
The matter is also complicated by the fact that in case of no matching files, the argument is passed without expansion. Therefor the problem of wildcard exansion will occur only in case that file-names matching the pattern exist.
I would also suspect different behaviour on different platforms in this case.
Expand/Shrink
Argumenty příkazové řádky:
python userinp2.1.py use*
Zdroj: (userinp2.1.py)
  1   import sys
  2   
  3   print sys.argv
  4   print sys.argv[0]
  5   print sys.argv[1:]
stdout:
['userinp2.1.py', 'userinp2.1.py']
userinp2.1.py
['userinp2.1.py']
Doba běhu: 20.9 ms
In this case matching file exists, expansion takes place.
Expand/Shrink
Argumenty příkazové řádky:
python userinp2.1.py wha*
Zdroj: (userinp2.1.py)
  1   import sys
  2   
  3   print sys.argv
  4   print sys.argv[0]
  5   print sys.argv[1:]
stdout:
['userinp2.1.py', 'wha*']
userinp2.1.py
['wha*']
Doba běhu: 21.2 ms
In this case no matching file exists, expansion does not take place.
The solution to this problem is again in quoting of the arguments.
Expand/Shrink
Argumenty příkazové řádky:
python userinp2.1.py "use*"
Zdroj: (userinp2.1.py)
  1   import sys
  2   
  3   print sys.argv
  4   print sys.argv[0]
  5   print sys.argv[1:]
stdout:
['userinp2.1.py', 'use*']
userinp2.1.py
['use*']
Doba běhu: 20.9 ms