Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109222

Re: How can I debug silent failure - print no output

From cs@zip.com.au
Newsgroups comp.lang.python
Subject Re: How can I debug silent failure - print no output
Date 2016-05-29 08:43 +1000
Message-ID <mailman.16.1464476343.1839.python-list@python.org> (permalink)
References <962627be-d620-4ba1-9429-68ed3f792b42@googlegroups.com> <20160528224313.GA38853@cskk.homeip.net>

Show all headers | View raw


On 28May2016 03:32, Sayth Renshaw <flebber.crue@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@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@zip.com.au>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I debug silent failure - print no output Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-27 19:41 -0700
  Re: How can I debug silent failure - print no output Michael Torrie <torriem@gmail.com> - 2016-05-27 21:06 -0600
    Re: How can I debug silent failure - print no output Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-27 21:02 -0700
      Re: How can I debug silent failure - print no output cs@zip.com.au - 2016-05-28 14:35 +1000
        Re: How can I debug silent failure - print no output Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-27 23:35 -0700
          Re: How can I debug silent failure - print no output Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-28 00:14 -0700
            Re: How can I debug silent failure - print no output Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-28 01:01 -0700
              Re: How can I debug silent failure - print no output Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-28 02:44 -0700
                Re: How can I debug silent failure - print no output Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-28 03:32 -0700
                Re: How can I debug silent failure - print no output cs@zip.com.au - 2016-05-29 08:43 +1000
          Re: How can I debug silent failure - print no output Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-05-28 12:52 -0400
  Re: How can I debug silent failure - print no output Jason Friedman <jsf80238@gmail.com> - 2016-05-27 22:02 -0600

csiph-web