Search code examples
pythonargumentsanacondaparameter-passingprompt

Passing multiple text files as arguments to a script using a pattern


First of all I'd like to state that this is a debug question for an exercise, but I can't get any help from the lecturer, and as much as I've read up on arguments I can't seem to figure it out, so here I am. So I have a python script that compares .txt files passed as arguments. Currently it is called it as follows:

python compare.py -s stop_list.txt NEWS/news01.txt NEWS/news02.txt

and the files are parsed into a list of names using

import sys, re, getopt, glob

opts, args = getopt.getopt(sys.argv[1:],'hs:bI:')
opts = dict(opts)
filenames = args

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

print('INPUT-FILES:', ' '.join(filenames))
print(filenames)

I can pass more than two files by simply listing them together

python compare.py -s stop_list.txt NEWS/news01.txt NEWS/news02.txt NEWS/news03.txt NEWS/news04.txt

but this can quickly become impractical.

Now it is suggested that more files can be passed using a pattern

python compare.py -s stop_list.txt -I ’NEWS/news??.txt’
i.e.:
python compare.py -s stop_list.txt -I ’NEWS/news0[123].txt’

However it seems to behave a bit weirdly. First of all if I write:

python compare.py -s stop_list.txt -I NEWS/news01.txt NEWS/news02.txt

only news01.txt will be passed to the script.

Following, when using the pattern as suggested there is no input whatsoever. I can't really understand if the code for parsing the input files is wrong and needs some altering, or I'm doing something wrong.

The -h states:

USE: python <PROGNAME> (options) file1...fileN
OPTIONS:
    -h : print this help message
    -b : use BINARY weights (default: count weighting)
    -s FILE : use stoplist file FILE
    -I PATT : identify input files using pattern PATT, 
              (otherwise uses files listed on command line)

Thanks in advance :)


Solution

  • Check the quotes. They seem special. Try ' or ", instead.