I'm writing this procedure
def get_special_paths(dir):
detected_paths = []
paths = os.listdir(dir)
for path in paths:
if path == r'__\w+__':
detected_paths.append(path)
for element in detected_paths:
index = detected_path.index(element)
detected_paths[index] = os.path.abspath(element)
return detected_paths
and it raises a a type error as below:
Traceback (most recent call last):
File"copyspecial.py", line 65, in <module>
get_special_paths(dir)
File"copysepcial.py", line 23, in get_special_paths
paths = os.listdir(pathname)
TypeError: coercing to Unicode: need string or buffer, builtin_function_or_method found
What's the meaning of that error and how do I fix it? Thanks in advance :)
It seems like you passed the dir builtin function to the get_special_paths
>>> dir
<built-in function dir>
>>> os.listdir(dir)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, builtin_function_or_method found
Pass the path as string.
get_special_paths('/path/to/dir')
BTW, don't use dir as a variable name. It will shadow the above dir function.
May be because global_path is not defined here:
for element in detected_paths:
index = detected_path.index(element) # Here detected_path is undefined
make it global_paths and try:
for element in detected_paths:
index = detected_paths.index(element)
Related
I have the following lines in a script I am running:
api_xml = os.path.join(opts.out, os.path.basename(
opts.api_raw).replace('.raw', '.xml'))
Running with Python 3.7, I get the error:
Traceback (most recent call last):
File "generate_code.py", line 32, in <module>
opts.api_raw).replace('.raw', '.xml'))
File "/usr/lib/python3.7/posixpath.py", line 146, in basename
p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType
It seems to me like a simple join and then replace, not sure why it is failing.
TypeError: expected str, bytes or os.PathLike object, not NoneType
means that you passed None to a function that expects a path.
Try adding these lines before you try to build api_xml:
assert opts.out is not None
assert opts.api_raw is not None
Traceback (most recent call last):
File "C:\Python27\meanshift.py", line 15, in <module>
roi = frame[r:r+h, c:c+w],
TypeError: 'NoneType' object has no attribute '__getitem__'
Please help me to resolve this error, I am trying to make a region of interest on the first frame to track an object.
Usually I get this kind of errors if I forgot to put a return statement at the end of a function:
def create_array(n):
a = np.array((n,5))
b = create_array(10)
print b[5,1]
Without an explicit return a at the end of the function, it will return None.
If I want to see the str.replace() function: help(str.replace)
the result is:
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
(END)
but how use help file.read or readlines?
for example, help(file.read) and help(read) are both errors:
>>> help(file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(file.read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'read' is not defined
How can I use help see file functions?
The file type has been removed from Python 3. Look at the io module instead:
>>> import io
>>> help(io.TextIOBase.read)
Help on method_descriptor:
read(...)
Read at most n characters from stream.
Read from underlying buffer until we have n characters or we hit EOF.
If n is negative or omitted, read until EOF.
I trying to open file and met some problem:
TypeError: coercing to Unicode: need string or buffer, NoneType found
Here is the code example:
a = open(fname, "rb").read(255)
Whats wrong with the code?
fname is None, not a string:
>>> open(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, NoneType found
You'll have to fix how you set fname or explicitly guard against it being None.
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 !
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.
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