[Baypiggies] importing variables into python namespace using argparse module

Simeon Franklin simeonf at gmail.com
Thu Sep 8 00:35:01 CEST 2011


Shiqi is correct - because you specified that the argument was of type file
argparse already opened the file for you. If you want to handle file opening
yourself specify str arguments instead.

Also - because you specified nargs the resulting variable is a list, even if
nargs=1. Just leave nargs off of the add_argument call if you only want one
argument. You can print the args variable when you execute the script to see
what its contents look like. I'd also strongly recommend Doug Hellman's
Python Module of the Week as a good source of tutorial style documentation.
The argparse chapter is at http://www.doughellmann.com/PyMOTW/argparse/

-regards
Simeon Franklin
209 846-2151

On Wed, Sep 7, 2011 at 3:15 PM, Abhishek Pratap <abhishek.vit at gmail.com>wrote:

> Hi Eric
>
> Sorry I did not provide the full info. Here is the exact code with
> full traceback at the end.
>
>
> import sys
> sys.path.append('/house/homedirs/a/apratap/lib/python');
> import argparse
>
>
>
> def read_file(file):
>    """Read the first same and create a dictionary
>    """
>     with open(file,'r') as fh1:
>        for line in fh1:
>            line.strip()
>            temp = line.split("\t")
>            print 'header : %s \t flag : %s' % (temp[0],temp[1])
>
>
>
>
> def compare_with_second_sam_file(file):
>    pass
>
>
>
> parser = argparse.ArgumentParser(description='Process some integers')
>
> parser.add_argument('--sam_file_1', nargs=1 , type=file,
> required=True,  help='Name of sam file 1')
> parser.add_argument('--sam_file_2', nargs=1 , type=file,
> required=True,  help='Name of sam file 2')
>
> args=parser.parse_args()
>
> sam_file_1 = args.sam_file_1
> sam_file_2 = args.sam_file_2
>
> print 'sam _file_1 has the value %s' % sam_file_1
> print 'type of var %s' % type(sam_file_1)
>
> read_file(sam_file_1)
>
>
> ###
> Traceback
> ###
>
>
> sam _file_1 has the value [<open file '1.sam', mode 'r' at 0x237db58>]
> type of var <type 'list'>
> Traceback (most recent call last):
>  File
> "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",
> line 41, in <module>
>     read_file(sam_file_1)
>   File
> "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",
> line 13, in read_file
>    with open(file,'r') as fh1:
> TypeError: coercing to Unicode: need string or buffer, list found
>
>
>
>
>
> On Wed, Sep 7, 2011 at 3:08 PM, Eric Walstad <eric at ericwalstad.com> wrote:
> > Hi Abhishek,
> >
> > In the future please include the full error traceback.  It includes
> > information that helps in debugging.
> >
> > It looks like your sam_file_1 variable contains a list when a string
> > is expected.  You could try printing the value of that variable before
> > passing it to your read_file function to see what it contains.  You've
> > changed your code and didn't include all the changes.
> >
> > Eric.
> >
> >
> > 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
> >
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110907/3a47a7e0/attachment-0001.html>


More information about the Baypiggies mailing list