Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple files with asterisk to python shell in Windows

I'm going through Google's Python exercises and I need to be able to do this from the command line:

python babynames.py --summaryfile baby*.html

Where python is the Python shell, babynames.py is the Python program, --summaryfile is an argument to be interpreted by my babynames program, and baby*.html is the list of files matching that expression. However, it doesn't work and I'm not sure if the problem is the Windows command shell or Python. The baby*.html expression is not being expanded out to the full list of files, instead it's being passed strictly as a string. Can multiple files be passed to a Python program in such a way?

like image 219
Mike Avatar asked Sep 19 '12 19:09

Mike


1 Answers

Windows' command interpreter does not expand wildcards as UNIX shells do before passing them to the executed program or script.

python.exe -c "import sys; print sys.argv[1:]" *.txt

Result:

['*.txt']

Solution: Use the glob module.

from glob import glob
from sys import argv

for filename in glob(argv[1]):
    print filename
like image 95
kindall Avatar answered Sep 29 '22 13:09

kindall