1

This is my first time using python's argparse and I am not sure where am I doing wrong. Here is my code:

#!/usr/bin/env python import argparse parser = argparse.ArgumentParser(description='Example of argparse') parser.add_argument('-i','--input', help='Input fasta file', required=True) parser.add_argument('-o','--output', help='Output text file', required=True) args = parser.parse_args() genes = {} # empty list with open(input, 'r') as fh_in: for line in fh_in: line = line.strip() if line[0] == ">": gene_names = line[1:] genes[gene_names] = '' else: genes[gene_names]+=line for (name,val) in genes.items(): with open(output, "w") as fh_out: val = len(val) fh_out.write('%s %s' % (name,val)) fh_out.write("\n") 

And when i try to run it i get this error

python get_gene_length.py -i test_in.fa -o test_out.txt Traceback (most recent call last): File "get_gene_length.py", line 13, in <module> with open(input, 'r') as fh_in: TypeError: coercing to Unicode: need string or buffer, builtin_function_or_method found 

Can anyone help me understand where should I make changes to the script to make it work?

2 Answers 2

2

The arguments are parsed into a namespace object args. You'll need to use args.input instead of input, which is a name already referring to a built-in function.

Similarly for opening the output file later on.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked after changing the input to args.input and similarly for output. This is great help!
2

You never define the variable input anywhere, but then use it in your code. However, input is the name of a built in python function, resulting in this error instead of a NameError.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.