IOError with Python and OpenCV - python

I want to do some OpenCV Basic Operations using Python. My Problem is that the Pythoninterpreter says that the file I want to open with cv.LoadImage() dont exists.
But as you can see in my code and the and the Interpreter Output this file exists and the Program should be able to read it.
Likly the answer is simple (I am new at Python Programming!).
Thanks for answers!
Here my Code:
import cv, sys, os
print sys.argv[1]
print os.getcwd()
print os.access(sys.argv[1], os.F_OK)
img = cv.LoadImage(sys.argv[1], 1)
cv.NamedWindow("orginal", CV_WINDOW_AUTOSIZE)
cv.ShowImage("orginal", img)
cv.waitKey(0)
here is the Pythoninterpreter Output:
dennis#Powertux:~/opencv/showPicture$ python2.5 showPicture.py google-de02.jpg google-de02.jpg
/home/steffke/opencv/showPicture
True
Traceback (most recent call last):
File "showPicture.py", line 7, in <module>
img = cv.LoadImage(sys.argv[1], 1)
IOError: [Errno 2] No such file or directory: 'google-de02.jpg'

I've tried both suggestions, but its the same like before. the os.access returns a TRUE but the function cv.LoadImage produce the same error.
Any other solutions?
anyhow thanks fpr answer...

Try giving it the whole path instead of the name only, or maybe using .\google-de02.jpg.

Try using the repr function when the exception happens.

Related

why is the error thinking Its not string?

path of file is:
"C:\Users\deana\OneDrive\Marlon's files\Programming\Python\PITT\PITT_LIbrary\Lists\test.txt"
lines of code are:
import os
os.chdir("C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists")
exec(open('test.txt'))
the error is this:
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
exec(open('test.txt'))
TypeError: exec() arg 1 must be a string, bytes or code object
also if I try on one line as such:
exec(open(r"C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists/test.txt"))
i'ts the same error. (with and without r)
super frustrationg as it reads like i'm not inputting string... but it is string!?!
also I've done this litteraly the same way before, restarted IDLE shell, no difference.
ugh! I always get stupid errors with file paths.
I should have been using os.startfile() to open this.
It was confusing by using .open(). as I was attempting to open in default app.
before, i've used exec.open() to open .py files and guess I got them confused.
exec is just used to open other scripts... need stronger coffee next time.
Try this:
import os
os.chdir("C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists")
exec(open('test.txt', 'rb'))
You can convert the txt file to bytes by opening it with rb (read bytes).

How to fix this error while executing a program

C:\Users\skandregula\AppData\Local\Programs\Python\Python37\python.exe
C:/Users/skandregula/Desktop/TestFiles2/testing.py
Traceback (most recent call last):
File "C:/Users/skandregula/Desktop/TestFiles2/testing.py", line 30, in <module>
with open(in_dir + f, 'r') as tmp_file:
FileNotFoundError: [Errno 2] No such file or directory:
'C:/Users/skandregula/Desktop/TestFiles2/history.log.3.3C'
Process finished with exit code 1
that is the error i am running right now...How to fix this?
Without seeing what your code looks like, I'm assuming somewhere in your code you're referencing a file or folder named 'history.log.3.3C' as the error suggests, and that it cannot find it in your directory given. My best advice is make sure the directory is correct?
Hard to tell without seeing what you're trying to do. Maybe posting a snippet of your code would help.

Is this the right way to use open() in python? its showing an error

Using the code below:
txt = open("/data/cards/deckofcards.txt").read().splitlines()
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/data/cards/deckofcards.txt'
txt=open("/home/vamsi/data/cards/deckofcards.txt").read().splitlines()
print(v)
I missed the (/home/vamsi) path.I misunderstood that pwd is by default and no need to give it in open().I figured it out and it is working fine.in open() we should also give pwd also to open the (.txt,.tsv etc..)file as we needed.
thank you,
screen shot of terminal
Give the absolute path of the file.
It would be something like C:/Desktop/data/card.txt.
The directory you mentioned does not exist and hence it is giving you that error.

Error when using Matplotlib.image in Python

Okay, so I just started this tutorial using Anaconda in PyCharm. I imported the correct libraries, but when I try to upload my image using this code:
img = mpimg.imread('FileName.png')
I get this error message:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File ".../matplotlib/image.py", line 1323, in imread
with open(fname, 'rb') as fd:
IOError: [Errno 2] No such file or directory: 'FileName.png'
I tried putting in the file location like this:
img=mpimg.imread('FilePath/FileName.png')
But I got the same error. I want to follow the tutorial exactly so I'm not sure why this isn't working! Any help is appreciated!
Make sure that there is that file in the folder...
Try this command to list the files in that folder just to make sure that python atleast recognizes or reads the files.
import os
print (os.listdir('your path'))
you should give an absolute path for the image. for example
r'C:\Users\HP\Desktop\stinkbug.png' adding r to remove the Unicode error
I was providing the full path: "C:/user/username/Downloads/folder/file.ext"
When I changed it to relative, it worked for me: "Downloads/folder/file.ext"

Why is the information for pywintypes.com_error unreadable?

Under Python 2.7.2 with pywin32-216.win32-py2.7 installed, when I use win32com module to process Excel on windows as follows:
>>> import win32com.client
>>> xlsApp = win32com.client.Dispatch('Excel.Application')
>>> xlsApp.Workbooks.Open(r'D:/test.xls')
I get an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, '\xb7\xa2\xc9\xfa\xd2\xe2\xcd\xe2\xa1\xa3',
(0, u'Microsoft Office Excel', u'\u540d\u4e3a\u201ctest.xls\u201d\u7684\u6587\u6
863\u5df2\u7ecf\u6253\u5f00\u3002\u4e0d\u80fd\u540c\u65f6\u6253\u5f00\u540c\u540
d\u6587\u4ef6\uff0c\u65e0\u8bba\u5b83\u4eec\u662f\u5426\u5728\u540c\u4e00\u6587\
u4ef6\u5939\u4e2d\u3002\n\u8981\u6253\u5f00\u7b2c\u4e8c\u4efd\u6587\u6863\uff0c\
u8bf7\u5173\u95ed\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u6863\uff0c\u6216\u8005\u9
1cd\u65b0\u547d\u540d\u5176\u4e2d\u7684\u4e00\u4e2a\u6587\u6863\u3002', None, 0,
-2146827284), None)
While the imformation is not readable, I don't konw what's going wrong!
After searching on the Internet, I find something helpful at http://www.python-forum.org/pythonforum/viewtopic.php?f=15&t=17665:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0,
u'Microsoft Office Excel', u"'test .xls' could not be found. Check the
spelling of the file name, and verify that the file location is
correct.\n\nIf you are trying to open the file from your list of most
recently used files on the File menu, make sure that the file has not
been renamed, moved, or deleted.", u'C:\Program Files\Microsoft
Office\OFFICE11\1033\xlmain11.chm', 0, -2146827284), None)
I guess it's the same problem, so I create an Excel file 'D:/test.xls' first, and then everything turns ok:
>>> xlsApp.Workbooks.Open(r'D:/test.xls')
<COMObject Open>
If I got the readable error tip, I would solve the problem immediately without any difficult!
I wonder why is the error I get from win32com.client like that? Is there anything I can do to make the information readable?
I will appreciate your help!
I imagine that it has come out like that because you are using internationalization settings for somewhere in Far East Asia (China, perhaps?) and you're using Python in the Command Prompt. I'm not sure whether the problem is with Python, the Command Prompt or the combination of the two, but either way the combination of these two doesn't work particularly well with non-Latin internationalization settings.
I'd recommend using IDLE instead, since that appears to support Unicode characters properly. Here's what happens when I viewed that last string in IDLE. The text doesn't mean anything to me, but it might do to you:
IDLE 2.6.4
>>> z = u'\u540d\u4e3a\u201ctest.xls\u201d\u7684\u6587\u6863\u5df2\u7ecf\u6253\u5f00\u3002\u4e0d\u80fd\u540c\u65f6\u6253\u5f00\u540c\u540d\u6587\u4ef6\uff0c\u65e0\u8bba\u5b83\u4eec\u662f\u5426\u5728\u540c\u4e00\u6587\u4ef6\u5939\u4e2d\u3002\n\u8981\u6253\u5f00\u7b2c\u4e8c\u4efd\u6587\u6863\uff0c\u8bf7\u5173\u95ed\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u6863\uff0c\u6216\u8005\u91cd\u65b0\u547d\u540d\u5176\u4e2d\u7684\u4e00\u4e2a\u6587\u6863\u3002'
>>> print z
名为“test.xls”的文档已经打开。不能同时打开同名文件,无论它们是否在同一文件夹中。
要打开第二份文档,请关闭已经打开的文档,或者重新命名其中的一个文档。
>>>
However, even when using IDLE, chances are that when you get the exception, the text will still appear as it did above. If this happens, what you need to do is to get the data from the last exception raised and print the relevant string from within it.
To get the last exception raised in the interpreter, you can use sys.last_value. Here's an example with a different exception message:
>>> import sys
>>> with open('nonexistent.txt') as f: pass
...
Traceback (most recent call last):
File "", line 1, in
with open('nonexistent.txt') as f: pass
IOError: [Errno 2] No such file or directory: 'nonexistent.txt'
>>> sys.last_value
IOError(2, 'No such file or directory')
>>> print(sys.last_value[1])
No such file or directory
>>>
I experienced a similar error. In my particular case I was trying to use a temporary file created like this:
fileprefix = 'Report'
filesuffix = '.xlsx'
filename = tempfile.gettempdir() + '\\' + fileprefix + filesuffix
xfile = tempfile.NamedTemporaryFile(suffix = filesuffix, prefix = fileprefix)
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
So, it seems that I cannot re-use that file in Windows OS. And that is the reason that I am getting the error.
In your case, you may want to check how the file is being created in the first place.

Categories