Python Forum

Full Version: Problem with reading a path
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
i'm having a problem on reading a path that contains a group of images, i have the paths on my desktop but i can't read them

Can you guys help me?

I have the exact code on a computers friend and it runs perfeclty

where is the code

from sklearn.decomposition import NMF
from matplotlib import pyplot as plt
import cv2
import numpy as np
import os
import sys
import statistics

## upload imgs e returns names e imgs
def uploadImg(dir1,dir2):
    cityscape_original = []
    landscape_original = []
    nameList = [[] for _ in range(512)]
    imgList = [[] for _ in range(512)]
    for file in os.listdir('./Desktop/landscape'):
        if file.endswith(".jpg"):
            nameList[0].append(file)
            
            orig = cv2.imread('./Desktop/landscape' + file)
            orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGB)
            cityscape = cv2.resize(orig,(512,512))
            #cityscape_original.append(cityscape)
            imgList[0].append(cityscape)
    for file in os.listdir('./Desktop/cityscape'):
        if file.endswith(".jpg"):
            nameList[1].append(file)
            orig = cv2.imread('./sktop/cityscape' + file)
            orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGB)

            landscape = cv2.resize(orig,(512,512))
            #landscape_original.append(landscape)
            imgList[1].append(landscape)
    return nameList,imgList


dir1 = os.listdir('./Desktop/cityscape')
dir2 = os.listdir('./Desktop/landscape')
name,img = uploadImg(dir1, dir2)
And this is my error

Error:
FileNotFoundError Traceback (most recent call last) <ipython-input-8-d8d91223d726> in <module> ----> 1 dir1 = os.listdir('./Desktop/cityscape') 2 dir2 = os.listdir('./Desktop/landscape') 3 name,img = uploadImg(dir1, dir2) FileNotFoundError: [WinError 3] The system cannot find the path specified: './Desktop/cityscape'
thanks for the support
Have you tried those paths without the initial './'?
yes, i've tried without the initial './' and i've got the same error
What's the absolute path of where you're running the python file? That could be different than wherever the file is saved, especially if you're running it from an ide.

Is the path you're running it from your Desktop's parent folder (which I think is just your user folder)?
(Jan-04-2019, 03:59 PM)nilamo Wrote: [ -> ]What's the absolute path of where you're running the python file? That could be different than wherever the file is saved, especially if you're running it from an ide.

Is the path you're running it from your Desktop's parent folder (which I think is just your user folder)?

Where do i find the absolute path?

Sorry for the question but i'm really new with python and this kind of stuff is hard to me xD

ps: I'm using Jupiter as Ide
(Jan-04-2019, 04:08 PM)gkiller007 Wrote: [ -> ]Where do i find the absolute path?

import os
import sys

try:
    root = os.path.dirname(os.path.abspath(__file__))
except NameError:
    import sys
    root = os.path.dirname(os.path.abspath(sys.argv[0]))

print (root)
(Jan-04-2019, 04:28 PM)Axel_Erfurt Wrote: [ -> ]
(Jan-04-2019, 04:08 PM)gkiller007 Wrote: [ -> ]Where do i find the absolute path?
 import os import sys try: root = os.path.dirname(os.path.abspath(__file__)) except NameError: import sys root = os.path.dirname(os.path.abspath(sys.argv[0])) print (root) 

I've done it and the result was this:

C:\Users\gonca\Anaconda3\lib\site-packages


What do i do with this?
Do you saved your file in C:\Users\gonca\Anaconda3\lib\site-packages ?

If so, that's not a good idea.
(Jan-04-2019, 04:41 PM)Axel_Erfurt Wrote: [ -> ]Do you saved your file in C:\Users\gonca\Anaconda3\lib\site-packages ? If so, that's not a good idea.

No, it is saved at desktop, the project itself and the paths that i'm trying to read

The result is the same

C:\Users\gonca\Anaconda3\lib\site-packages
So the files you're trying to open are in C:\Users\gonca\Anaconda3\lib\site-packages\Desktop\cityscape and C:\Users\gonca\Anaconda3\lib\site-packages\Desktop\landscape, correct?

If not, then I'd suggest either using os.chdir() to change where you think you are to match where your file actually is, or using absolute paths to reference your files/folders.

Here's an example of using os.chdir():
import os
import pathlib

current_file = pathlib.Path(__file__)
current_location = current_file.parent.absolute()
os.chdir(current_location)

# references from here on out to relative paths will be relative to wherever this file exists
print(os.getcwd())
Pages: 1 2 3 4