Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Trouble porting glob bash behavior with argparse to windows shell Date: Mon, 16 May 2016 08:58:50 +0200 Organization: None Lines: 62 Message-ID: References: <80f6672a-2b69-4749-821d-a92be107862a@googlegroups.com> <664703cb-ac1f-411d-a0d7-0a50a683d2fb@googlegroups.com> <125a6208-8752-482e-ac8f-8ef2be012dae@googlegroups.com> <8d064bd8-5c4e-4693-97d0-2bff5a102ee3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de LlJIDA1jUlwa62IZLmzeEA4/9ggfduX3jlqNfAxANoaA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.04; 'wednesday,': 0.07; 'instance.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'causing': 0.13; 'def': 0.13; 'suggest': 0.15; 'argument': 0.15; '"*"': 0.16; '2016': 0.16; 'argparse': 0.16; 'attr': 0.16; 'confusion': 0.16; 'executed.': 0.16; 'expect,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'set()': 0.16; 'sqlite3': 0.16; 'statement.': 0.16; 'subject:argparse': 0.16; 'subject:windows': 0.16; 'wrote:': 0.16; 'later': 0.16; 'meetings': 0.18; 'variable': 0.18; 'extension': 0.20; 'arguments': 0.22; 'function:': 0.22; 'parser': 0.22; 'seems': 0.23; 'import': 0.24; 'xml': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'header:X -Complaints-To:1': 0.26; 'error': 0.27; 'skip:f 40': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'values': 0.28; 'actual': 0.28; 'attempting': 0.29; 'implicitly': 0.29; 'meeting': 0.30; 'creating': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'noticed': 0.32; 'problem': 0.33; 'file': 0.34; 'list': 0.34; 'filter': 0.35; 'path': 0.35; 'created': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'files': 0.38; 'well.': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'your': 0.60; 'skip:n 10': 0.62; 'phone.': 0.63; 'accessed': 0.66 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8975.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <80f6672a-2b69-4749-821d-a92be107862a@googlegroups.com> <664703cb-ac1f-411d-a0d7-0a50a683d2fb@googlegroups.com> <125a6208-8752-482e-ac8f-8ef2be012dae@googlegroups.com> <8d064bd8-5c4e-4693-97d0-2bff5a102ee3@googlegroups.com> Xref: csiph.com comp.lang.python:108659 Sayth Renshaw wrote: > On Wednesday, 4 May 2016 17:57:32 UTC+10, Sayth Renshaw wrote: >> Oops sorry noticed you did in the glob. Sorry squinting at phone. >> >> Sayth > > Hi > > this seems to be causing me an error in my thinking as well as the > program. I am creating a function GetArgs to take a path and file > extension from the command line. > > However I cannot call it effectively. I will clrify this is my function > > import argparse > import glob > import os > import sqlite3 > > > def GetArgs(parser): > '''parse XML from command line''' > parser.add_argument("path", nargs="+") > parser.add_argument('-e', '--extension', default='', > help='File extension to filter by.') > args = parser.parse_args() > > files = set() > name_pattern = "*" + args.extension > for path in args.path: > files.update(glob.glob(os.path.join(path, name_pattern))) > return files > > Then later in program I am attempting to call it an a for statement. > > filesToProcess = GetArgs() > for meeting in filesToProcess: > meetdata = [meeting.get(attr) for attr in meetattrs] > cur.execute("insert into meetings values (" + > ",".join(["%s"] * len(meetattrs)) + ")", meetdata) > > > this fails as i would expect, however if I declare a list as the GetArgs() > argument it fails as well. > > Where my confusion is that I created the function to take arguments from > the command line, so I don't have that variable to supply until executed. > > Have i overbaked the cake? The actual arguments are in sys.argv and will be implicitly accessed by the parser.parse_args() method invocation. The problem is simply that you don't create an argparse.ArgumentParser() instance. I suggest that you do that inside the GetArgs() function: def GetArgs(): # no arguments parser = argparse.ArgumentParser() # your current code below parser.add_argument("path", nargs="+") ...