Python Forum

Full Version: Can't Find Path
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I get the following error {the last line shown} regarding the path. "The system cannot find the path specified: './Data/train' "

The location of the train folder is correct. Do I have the format wrong?

In the main file, main.py:

train_dir = os.path.join(data_dir, 'C:\Documents\Pyro\dogs-vs-cats\train')
train_dataset = datasetloader(train_dir, transform=train_transform)  **error**
In another file, dataset.py:

class datasetloader(Dataset):
    def __init__(self, path, transform=None):
        self.classes   = os.listdir(path)  ##the error causing line  **error**
train_dir = os.path.join(data_dir, 'C:\Documents\Pyro\dogs-vs-cats\train')

must be wrong, what's the path of data_dir?
>>> p = 'C:\Documents\Pyro\dogs-vs-cats\train'
>>> print(p)
C:\Documents\Pyro\dogs-vs-cats	rain
Now see the problem,so never single \ in path because of escape characters.
Add r or turn around /.
>>> p = r'C:\Documents\Pyro\dogs-vs-cats\train'
>>> print(p)
C:\Documents\Pyro\dogs-vs-cats\train

>>> p = r'C:/Documents/Pyro/dogs-vs-cats/train'
>>> print(p)
C:/Documents/Pyro/dogs-vs-cats/train
Yes, I tried the / and still same error.
(Oct-29-2023, 06:05 PM)hatflyer Wrote: [ -> ]I get the following error {the last line shown} regarding the path. "The system cannot find the path specified: './Data/train' "

The location of the train folder is correct. Do I have the format wrong?

In the main file, main.py:

train_dir = os.path.join(data_dir, 'C:\Documents\Pyro\dogs-vs-cats\train')
train_dataset = datasetloader(train_dir, transform=train_transform) **error**


In another file, dataset.py:

class datasetloader(Dataset):
def __init__(self, path, transform=None):
self.classes = os.listdir(path) ##the error causing line **error**

(Oct-29-2023, 07:47 PM)Axel_Erfurt Wrote: [ -> ]train_dir = os.path.join(data_dir, 'C:\Documents\Pyro\dogs-vs-cats\train')

must be wrong, what's the path of data_dir?

I tried this:

data_dir = 'C:/Documents/Pyro/dogs-vs-cats'
train_dir = os.path.join(data_dir, '/train')
test_dir = os.path.join(data_dir, '/test')

Still fails.
remove slash

train_dir = os.path.join(data_dir, 'train')
test_dir = os.path.join(data_dir, 'test')
Thanks much!
Some day, hopefully soon, the os module will go away. Use pathlib.
from pathlib import Path

data_dir = Path('C:/Documents/Pyro/dogs-vs-cats')
train_dir = data_dir / 'train'
test_dir = data_dir / 'test'
(Oct-29-2023, 11:37 PM)deanhystad Wrote: [ -> ]Some day, hopefully soon, the os module will go away.
I'm not in a hurry. When the os module goes away, a part of the soul of Python will go away. The os module has always been there since the 1990s. The os module is a low-level module that contains many interesting things. I don't think it is in conflict with higher level modules such as pathlib or subprocess. It should be used for specialized purposes only.