How can I debug silent failure - print no output

cs at zip.com.au cs at zip.com.au
Sat May 28 18:43:13 EDT 2016


On 28May2016 03:32, Sayth Renshaw <flebber.crue at gmail.com> wrote:
>On Saturday, 28 May 2016 19:44:53 UTC+10, Sayth Renshaw  wrote:
>> On Saturday, 28 May 2016 18:02:06 UTC+10, Sayth Renshaw  wrote:
>> > So how do i get argparse to read the file arguments correctly?
>> >
>> > Looking at the namespace it all gets pushed into path and extension remains empty.
>> >
>> > [sayth at localhost pyXML]$ python3 racemeeting.py data/ *.xml
>> > Namespace(extension='', path=['data/', '*.xml'])
>> > This is the section I am running
>> >
>> > parser = argparse.ArgumentParser()
>> > parser.add_argument("path", nargs="+")
>> > parser.add_argument('-e', '--extension', default='',
>> >                     help='File extension to filter by.')
>> >
>> > args = parser.parse_args()
>> > name_pattern = "*" + args.extension
>> > print(args)
>>
>> Ah if only i used argparse properly
>>
>> python racemeeting.py data/ -e *.xml

There are a couple of things here.

First is that a normal UNIX command puts all the options first, so you should 
be desiging your command line to run like this:

  python racemeeting.py -e *.xml data

or perhaps:

  python racemeeting.py -d data -e *.xml

It is traditional to stop parsing options such as -e when you reach the first 
non-option, because that lets one put whatever is necessary safely _after_ the 
options without fear that one of the arguments will resemble an option. For 
example, suppoing you have a file with the name "-e" and said:

  somecommand -f foo dir *

intending to use all the local filenames after "dir". In your current scheme 
(accepting options after "dir") a "-e" appearing later would be misinterpreted.

The second is to be aware that the shell expands globs _before_ invoking the 
command. This is extremely useful because it means that (a) commands usually 
don't need to do their own glob expansion and (b) all commands end up using the 
same glob syntax because it is common to the shell, not a command-specific 
special syntax. Which makes everything easier to use.

The upshot of that is that you should _either_ be quoting "*.xml" on your 
command line to prevent expansion, _or_ you should not be bothering with he 
asterisk, instead passing the argument ".xml" or even just "xml".

As an experiment, make two dummy XML files in your current directory (where 
your script is):

  >>dummy1.xml
  >>dummy2.xml

and run your script passing *.xml as you currently do, and see what your print 
statements say. This should make the effect obvious.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list