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&#39;d also strongly recommend Doug Hellman&#39;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">&lt;<a href="mailto:abhishek.vit@gmail.com">abhishek.vit@gmail.com</a>&gt;</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(&#39;/house/homedirs/a/apratap/lib/python&#39;);<br>
import argparse<br>
<div class="im"><br>
<br>
<br>
def read_file(file):<br>
    &quot;&quot;&quot;Read the first same and create a dictionary<br>
    &quot;&quot;&quot;<br>
</div>    with open(file,&#39;r&#39;) as fh1:<br>
        for line in fh1:<br>
            line.strip()<br>
            temp = line.split(&quot;\t&quot;)<br>
            print &#39;header : %s \t flag : %s&#39; % (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=&#39;Process some integers&#39;)<br>
<br>
</div>parser.add_argument(&#39;--sam_file_1&#39;, nargs=1 , type=file,<br>
required=True,  help=&#39;Name of sam file 1&#39;)<br>
parser.add_argument(&#39;--sam_file_2&#39;, nargs=1 , type=file,<br>
required=True,  help=&#39;Name of sam file 2&#39;)<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 &#39;sam _file_1 has the value %s&#39; % sam_file_1<br>
print &#39;type of var %s&#39; % 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 [&lt;open file &#39;1.sam&#39;, mode &#39;r&#39; at 0x237db58&gt;]<br>
type of var &lt;type &#39;list&#39;&gt;<br>
Traceback (most recent call last):<br>
  File &quot;/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py&quot;,<br>
line 41, in &lt;module&gt;<br>
<div class="im">    read_file(sam_file_1)<br>
</div>  File &quot;/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py&quot;,<br>
line 13, in read_file<br>
    with open(file,&#39;r&#39;) 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 &lt;<a href="mailto:eric@ericwalstad.com">eric@ericwalstad.com</a>&gt; wrote:<br>
&gt; Hi Abhishek,<br>
&gt;<br>
&gt; In the future please include the full error traceback.  It includes<br>
&gt; information that helps in debugging.<br>
&gt;<br>
&gt; It looks like your sam_file_1 variable contains a list when a string<br>
&gt; is expected.  You could try printing the value of that variable before<br>
&gt; passing it to your read_file function to see what it contains.  You&#39;ve<br>
&gt; changed your code and didn&#39;t include all the changes.<br>
&gt;<br>
&gt; Eric.<br>
&gt;<br>
&gt;<br>
&gt; On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap &lt;<a href="mailto:abhishek.vit@gmail.com">abhishek.vit@gmail.com</a>&gt; wrote:<br>
&gt;&gt; Guys<br>
&gt;&gt;<br>
&gt;&gt; I think I am stuck again while trying to open a file. It looks like a<br>
&gt;&gt; type casting error but not sure.<br>
&gt;&gt;<br>
&gt;&gt; ##Error:<br>
&gt;&gt; TypeError: coercing to Unicode: need string or buffer, list found<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; #code<br>
&gt;&gt;<br>
&gt;&gt; def read_file(file):<br>
&gt;&gt;    &quot;&quot;&quot;Read the first same and create a dictionary<br>
&gt;&gt;    &quot;&quot;&quot;<br>
&gt;&gt;    fh1 = open(file,&#39;r&#39;)<br>
&gt;&gt;    for line in fh1:<br>
&gt;&gt;        print line<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; sam_file_1 = args.sam_file_1<br>
&gt;&gt; sam_file_2 = args.sam_file_2<br>
&gt;&gt;<br>
&gt;&gt; read_file(sam_file_1)<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; -Abhi<br>
&gt;<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>