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


Groups > comp.lang.python > #108058 > unrolled thread

Trouble porting glob bash behavior with argparse to windows shell

Started bySayth Renshaw <flebber.crue@gmail.com>
First post2016-05-03 04:34 -0700
Last post2016-05-03 18:00 -0400
Articles 8 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Trouble porting glob bash behavior with argparse to windows shell Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-03 04:34 -0700
    Trouble porting glob bash behavior with argparse to windows shell Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-03 13:55 -0700
      Re: Trouble porting glob bash behavior with argparse to windows shell Peter Otten <__peter__@web.de> - 2016-05-03 23:15 +0200
        Re: Trouble porting glob bash behavior with argparse to windows shell Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-04 00:56 -0700
        Re: Trouble porting glob bash behavior with argparse to windows shell Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-04 00:57 -0700
          Re: Trouble porting glob bash behavior with argparse to windows shell Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-15 20:21 -0700
            Re: Trouble porting glob bash behavior with argparse to windows shell Peter Otten <__peter__@web.de> - 2016-05-16 08:58 +0200
      Re: Trouble porting glob bash behavior with argparse to windows shell Terry Reedy <tjreedy@udel.edu> - 2016-05-03 18:00 -0400

#108058 — Trouble porting glob bash behavior with argparse to windows shell

FromSayth Renshaw <flebber.crue@gmail.com>
Date2016-05-03 04:34 -0700
SubjectTrouble porting glob bash behavior with argparse to windows shell
Message-ID<80f6672a-2b69-4749-821d-a92be107862a@googlegroups.com>
Hi

I had a simple argparse working on ubuntu bash. However now I am trying to run the  script on windows and it cannot work because cmd doesn't handle the glob like bash does.

So I am attempting to modify my script to accommodate.

As i am running python 3.5 i can use glob.glob for a list of files I believe.

Now I am specifying my arguments as 2 arguments path and extension

python script.py /mypath/XML *xml

This is my current error I have had many.

TypeError was unhandled by user code
Message: can only concatenate list (not "str") to list
'
import argparse 
import glob


parser = argparse.ArgumentParser(description=None) 


def GetArgs(parser): 
    """Parser function using argparse""" 
    # parser.add_argument('directory', help='directory use',
    #                     action='store', nargs='*')
    
    parser.add_argument("path", nargs="+") 
    parser.add_argument('-e', '--extension', default='', help='File extension to filter by.')
    args = parser.parse_args()

    files = set()
    files |= set(glob.glob(args.path + '/*' + args.extension))
    return files


fileList = GetArgs(parser)
for file in fileList:
    print(file)


At this became unsure of where to troubleshoot further.

Sayth

[toc] | [next] | [standalone]


#108098

FromSayth Renshaw <flebber.crue@gmail.com>
Date2016-05-03 13:55 -0700
Message-ID<664703cb-ac1f-411d-a0d7-0a50a683d2fb@googlegroups.com>
In reply to#108058
Is there something obvious to this I am doing wrong? 

Sayth 

[toc] | [prev] | [next] | [standalone]


#108100

FromPeter Otten <__peter__@web.de>
Date2016-05-03 23:15 +0200
Message-ID<mailman.363.1462310161.32212.python-list@python.org>
In reply to#108098
Sayth Renshaw wrote:

> Is there something obvious to this I am doing wrong?

>     parser.add_argument("path", nargs="+") 
 
The "+" implicitly turns args.path into a list

>     files |= set(glob.glob(args.path + '/*' + args.extension))

so the glob() argument is evaluated as

list + str + str

Try

name_pattern = "*" + args.extension
for path in args.path:
    files.update(glob.glob(os.path.join(path, name_pattern)))

[toc] | [prev] | [next] | [standalone]


#108119

FromSayth Renshaw <flebber.crue@gmail.com>
Date2016-05-04 00:56 -0700
Message-ID<c13daf43-e5de-4e4d-a1f7-ea852f5bc563@googlegroups.com>
In reply to#108100
Thank you Peter. 
I was starting to flail and thought my use of glob.glob was wrong. 

As an aside should I be using os.path to negate system inconsistency? 

Thanks 

Sayth 

[toc] | [prev] | [next] | [standalone]


#108120

FromSayth Renshaw <flebber.crue@gmail.com>
Date2016-05-04 00:57 -0700
Message-ID<125a6208-8752-482e-ac8f-8ef2be012dae@googlegroups.com>
In reply to#108100
Oops sorry noticed you did in the glob. Sorry squinting at phone. 

Sayth 

[toc] | [prev] | [next] | [standalone]


#108656

FromSayth Renshaw <flebber.crue@gmail.com>
Date2016-05-15 20:21 -0700
Message-ID<8d064bd8-5c4e-4693-97d0-2bff5a102ee3@googlegroups.com>
In reply to#108120
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?

Sayth

[toc] | [prev] | [next] | [standalone]


#108659

FromPeter Otten <__peter__@web.de>
Date2016-05-16 08:58 +0200
Message-ID<mailman.10.1463381968.19823.python-list@python.org>
In reply to#108656
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="+")
    ...

[toc] | [prev] | [next] | [standalone]


#108102

FromTerry Reedy <tjreedy@udel.edu>
Date2016-05-03 18:00 -0400
Message-ID<mailman.365.1462312839.32212.python-list@python.org>
In reply to#108098
On 5/3/2016 4:55 PM, Sayth Renshaw wrote:
> Is there something obvious to this I am doing wrong?
>
> Sayth

Somethin happened so that I don't see what you did.  Fortunately, it did 
show up for Peter, between the '?' and name, so he could answer.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web