When I try to execute Python files from VS Code, it is unable to find other files in the same folder.
I am running OpenCV code in a test.py file with the following command :
filename = 'image1.png'
img1 = cv2.imread(filename)
print(img1)
The results should show an array of numbers but instead show None because VS Code cannot find image1.png which is in the same folder as the test.py file.
I have searched online for a solution but yet to find a clear one. I suspect I need to manually add this folder to a .json file, but this seems like a clunky solution for every time I need to run files all contained in the same folder.
(I support Monica - StackOverflows needs to stop being aggressive transgender crusaders.)
I think the folder you were at in the terminal is not this folder. If that's the case then use absolute path or path relative from projrct folder instead.
Change the image1.png
to ./image1.png
should do as well.
If the project folder looks like
projectfolder
|
subfolder
|
main.py
image.png
And the terminal is at projectfolder
then relative path of image.png
is projectfolder/image.png
.
Thogh, use ./image.png
is way less troublesome if the picture is always there, but if it's not then absolute path(like /home/user/projectfolder/folder/image.png
) will eventually reduce time needed for debug.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.