[Baypiggies] importing variables into python namespace using argparse module

Abhishek Pratap abhishek.vit at gmail.com
Thu Sep 8 00:15:14 CEST 2011


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
>


More information about the Baypiggies mailing list