[Baypiggies] importing variables into python namespace using argparse module

Abhishek Pratap abhishek.vit at gmail.com
Thu Sep 8 00:34:07 CEST 2011


I think I understand why the error happens but still not sure about a
step 2(below)

1. I was not aware that the object type returned by
parser.add_argument when "type=file" is a file handle. I took it as a
string or file name.

**Here is what I dont understand.**
2.
parser.add_argument('--sam_file_1', nargs=1 , type=file,
required=True,  help='Name of sam file 1')
sam_file_1 = args.sam_file_1

sam_file_1 is a list ?? I am not sure why

Does this mean all the arguments returned by parser.parse_args() are a
list even if they have one variable. May be I am missing a concept
here.

If I make the following change then the code works which is basically
extracting an element out of the list.
sam_file_1 = args.sam_file_1 [0]

-Abhi



On Wed, Sep 7, 2011 at 3:15 PM, Shiqi Yang <sherman.yang at gmail.com> wrote:
> It might be because the type is set to 'file' for args.sam_file_1 and
> then the file object was returned as sam_file_1
> then passed to open() in read_file()
>
> maybe try change type=str would help
>
> HTH,
> Shiqi
>
> On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap <abhishek.vit at gmail.com> wrote:
>> Guys
>>
>> I think I am stuck again while trying to open a file. It looks like a
>> type casting error but not sure.
>>
>> ##Error:
>> TypeError: coercing to Unicode: need string or buffer, list found
>>
>>
>> #code
>>
>> def read_file(file):
>>    """Read the first same and create a dictionary
>>    """
>>    fh1 = open(file,'r')
>>    for line in fh1:
>>        print line
>>
>>
>> sam_file_1 = args.sam_file_1
>> sam_file_2 = args.sam_file_2
>>
>> read_file(sam_file_1)
>>
>>
>> -Abhi
>>
>>
>> On Wed, Sep 7, 2011 at 2:01 PM, Abhishek Pratap <abhishek.vit at gmail.com> wrote:
>>> Thanks Simeon and Rami. Works now.
>>>
>>> -Abhi
>>>
>>>
>>>
>>>
>>> On Wed, Sep 7, 2011 at 1:55 PM, Rami Chowdhury <rami.chowdhury at gmail.com> wrote:
>>>> On Wed, Sep 7, 2011 at 21:11, Abhishek Pratap <abhishek.vit at gmail.com> wrote:
>>>>> This might sound naive as I am trying to learn python.
>>>>
>>>> Not at all, we've all been there :-)
>>>>
>>>>> I am using argparse to parse the command line arguments but I am not
>>>>> sure how they variables created by it are imported into python's
>>>>> current namespace for downstream usage.
>>>>
>>>> The simple answer is that they're not. I'll explain further after the
>>>> code sample
>>>>
>>>>> parser = argparse.ArgumentParser(description='Process some integers')
>>>>>
>>>>> parser.add_argument('--file_1', nargs=1 , type=file, required=True,
>>>>> help='Name of file 1')
>>>>> parser.add_argument('--file_2', nargs=1 , type=file, required=True,
>>>>> help='Name of file 2')
>>>>> parser.parse_args()
>>>>
>>>> Here, you need to capture the object that is returned from
>>>> parser.parse_args() -- that object is where any data captured by the
>>>> parser is stored, and you can access it from that object. So, for
>>>> instance:
>>>>
>>>>>>> args = parser.parse_args()
>>>>>>> print "File 1 is %s" % args.file_1
>>>>
>>>>> PS: Please let me know if this is not a appropriate forum to push such
>>>>> questions and if there are other mailing list that I could use coz in
>>>>> the coming days I am sure to send in a lot of email traffic.
>>>>
>>>> You could also try the main Python-language mailing list
>>>> (python-list at python.org) -- there are more people on the list and you
>>>> might get quicker responses to questions :-)
>>>>
>>>> Hope that helps,
>>>> Rami
>>>>
>>>> --
>>>> Rami Chowdhury
>>>> "Never assume malice when stupidity will suffice." -- Hanlon's Razor
>>>> +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544
>>>>
>>>
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/mailman/listinfo/baypiggies
>>
>


More information about the Baypiggies mailing list