2

I am trying to input a path using optparser in python. Unfortunately this piece of code keeps showing an error.

import optparse,os

parser = optparse.OptionParser()
parser.add_option("-p","--path", help = "Prints path",dest = "Input_Path", metavar = "PATH")

(opts,args) =parser.parse_args()

print os.path.isdir(opts.Input_Path)

Error :-

Traceback (most recent call last):
  File "/Users/armed/Documents/Python_Test.py", line 8, in 
    print os.path.isdir(opts.Input_Path)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py", line 41, in isdir
    st = os.stat(s)
TypeError: coercing to Unicode: need string or buffer, NoneType found

Any help is much appreciated !

4
  • Are you definitely calling your script with "-p something"? Commented Sep 21, 2011 at 17:47
  • Yes I am. I am inputting it as a string but i think it gets stored as a list because when I do a for file in opts.Input_Path and print file it shows the output letter by letter for whatever I am inputting. Do you know why that is ? i just want it to print the files in the directory the path is pointing to Commented Sep 21, 2011 at 17:49
  • A string is an iterable object, just like a list. "for a in a_string" will loop through the characters of the string. Instead of the for loop just use the attribute opts.Input_Path itself. Commented Sep 21, 2011 at 18:06
  • Here's the obligatory comment that optparse has been deprecated in favor of argparse. Commented Sep 21, 2011 at 18:18

2 Answers 2

3

That error is because opts.Input_Path is None, instead of being your path string/unicode.

Are you sure you are calling the script correctly? You should probably put in some error checking code in any case to make sure that if a user doesnt put -p, the program won't just crash.

Or, change it to a positional argument to make it 'required' by optparse: http://docs.python.org/library/optparse.html#what-are-positional-arguments-for

Edit: Also optparse is deprecated, for a new project you probably want to use argparse.

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

2 Comments

Will check it out ! Thanks :) Yes i was calling it the wrong way.
If this was the problem, you should mark one of these as an answer to let the community know : )
2

I copied your script and ran it. Looks like you call your script in a wrong way:

 $ python test.py /tmp
 Traceback (most recent call last):
   File "test.py", line 8, in <module>
     print os.path.isdir(opts.Input_Path)
   File "/usr/lib/python2.6/genericpath.py", line 41, in isdir
     st = os.stat(s)
 TypeError: coercing to Unicode: need string or buffer, NoneType found

but

$ python test.py --path /tmp
True

Comments

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.