4

I'm attempting to write a simple script that will take a filename from the command line and convert that file to a another format. Below is a small simple snippet of code that I am starting with but I keep getting this error: NameError: name 'file_name' is not defined

Here is the code, i'm on a Mac using python 2.7.10.

#!/usr/bin/env python
import sys
import argparse

parser = argparse.ArgumentParser(description='Convert Hex Files')
parser.add_argument('-f', dest=file_name, help='Please Enter the Path to Your File.', type=string)
parser.add_argument('-n', dest=line_length, help='Enter The Desired Line Length.', type=int)
args = parser.parse_args()

print args.file_name
print args.line_length

1 Answer 1

6

In the argparse module, the destinations must be strings.

Therefore, you need to quote the vales that you are assigning to dest.

dest = "file_name"
dest = "line_length"

See the documentation of this option for full details.

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

2 Comments

Thank you this worked, I should've read the API more closely. I also had to remove the 'type=string' because it's a string by default as you mentioned.
You're welcome. If this was helpful, please consider accepting this answer. You are not obligated to accept any answer, but that will mark the question as answered, and will award both the person who answered the question and yourself with a small amount of reputation. You can additionally upvote any answer that you find particularly useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.