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. <br><br>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 <a href="http://www.doughellmann.com/PyMOTW/argparse/">http://www.doughellmann.com/PyMOTW/argparse/</a><br>
<br>-regards<br>Simeon Franklin<br>209 846-2151<br><br><div class="gmail_quote">On Wed, Sep 7, 2011 at 3:15 PM, Abhishek Pratap <span dir="ltr"><<a href="mailto:abhishek.vit@gmail.com">abhishek.vit@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi Eric<br>
<br>
Sorry I did not provide the full info. Here is the exact code with<br>
full traceback at the end.<br>
<br>
<br>
import sys<br>
sys.path.append('/house/homedirs/a/apratap/lib/python');<br>
import argparse<br>
<div class="im"><br>
<br>
<br>
def read_file(file):<br>
"""Read the first same and create a dictionary<br>
"""<br>
</div> with open(file,'r') as fh1:<br>
for line in fh1:<br>
line.strip()<br>
temp = line.split("\t")<br>
print 'header : %s \t flag : %s' % (temp[0],temp[1])<br>
<br>
<br>
<br>
<br>
def compare_with_second_sam_file(file):<br>
pass<br>
<div class="im"><br>
<br>
<br>
parser = argparse.ArgumentParser(description='Process some integers')<br>
<br>
</div>parser.add_argument('--sam_file_1', nargs=1 , type=file,<br>
required=True, help='Name of sam file 1')<br>
parser.add_argument('--sam_file_2', nargs=1 , type=file,<br>
required=True, help='Name of sam file 2')<br>
<br>
args=parser.parse_args()<br>
<div class="im"><br>
sam_file_1 = args.sam_file_1<br>
sam_file_2 = args.sam_file_2<br>
<br>
</div>print 'sam _file_1 has the value %s' % sam_file_1<br>
print 'type of var %s' % type(sam_file_1)<br>
<div class="im"><br>
read_file(sam_file_1)<br>
<br>
<br>
</div>###<br>
Traceback<br>
###<br>
<br>
<br>
sam _file_1 has the value [<open file '1.sam', mode 'r' at 0x237db58>]<br>
type of var <type 'list'><br>
Traceback (most recent call last):<br>
File "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",<br>
line 41, in <module><br>
<div class="im"> read_file(sam_file_1)<br>
</div> File "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",<br>
line 13, in read_file<br>
with open(file,'r') as fh1:<br>
<div class="im">TypeError: coercing to Unicode: need string or buffer, list found<br>
<br>
<br>
<br>
<br>
<br>
</div><div><div></div><div class="h5">On Wed, Sep 7, 2011 at 3:08 PM, Eric Walstad <<a href="mailto:eric@ericwalstad.com">eric@ericwalstad.com</a>> wrote:<br>
> Hi Abhishek,<br>
><br>
> In the future please include the full error traceback. It includes<br>
> information that helps in debugging.<br>
><br>
> It looks like your sam_file_1 variable contains a list when a string<br>
> is expected. You could try printing the value of that variable before<br>
> passing it to your read_file function to see what it contains. You've<br>
> changed your code and didn't include all the changes.<br>
><br>
> Eric.<br>
><br>
><br>
> On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap <<a href="mailto:abhishek.vit@gmail.com">abhishek.vit@gmail.com</a>> wrote:<br>
>> Guys<br>
>><br>
>> I think I am stuck again while trying to open a file. It looks like a<br>
>> type casting error but not sure.<br>
>><br>
>> ##Error:<br>
>> TypeError: coercing to Unicode: need string or buffer, list found<br>
>><br>
>><br>
>> #code<br>
>><br>
>> def read_file(file):<br>
>> """Read the first same and create a dictionary<br>
>> """<br>
>> fh1 = open(file,'r')<br>
>> for line in fh1:<br>
>> print line<br>
>><br>
>><br>
>> sam_file_1 = args.sam_file_1<br>
>> sam_file_2 = args.sam_file_2<br>
>><br>
>> read_file(sam_file_1)<br>
>><br>
>><br>
>> -Abhi<br>
><br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>