文件路径无法正确读取

时间:2018-10-15 22:07:11

标签: python keras

我是DL的新手,我正在读一本书来构建我的第一个CNN。事情是(而且我已经尝试了各种方法)我无法打开文件以将一部分图像传输到较小的数据集中。以下是代码和尝试运行程序时遇到的错误。我已经尝试过以我能想到的各种方式来改变反弹,但是我似乎无法让程序检查到catsvdogs文件夹的子文件夹中。

代码:

from itertools import groupby
from operator import itemgetter

res = [list(g)[0] for _, g in groupby(lst, key=itemgetter('Key'))]

我得到的错误:

import os, shutil


# Path to the directory where the original DS was uncompressed

original_dir = 'datasets/catsvdogs'
# Directory to store my smaller dataset

base_dir = 'datasets/catsvdogs_small'
os.mkdir(base_dir)

# Directories for the training, validation, and test splits

train_dir = os.path.join(base_dir, 'train')
os.mkdir(train_dir)
validation_dir = os.path.join(base_dir, 'valid')
os.mkdir(validation_dir)
test_dir = os.path.join(base_dir, 'test')
os.mkdir(test_dir)

# Directory with training cat pictures

train_cats_dir = os.path.join(train_dir, 'cats')
os.mkdir(train_cats_dir)

# Directory with training dog pictures 

train_dogs_dir = os.path.join(train_dir, 'dogs')
os.mkdir(train_dogs_dir)

# Directory with validation cat pictures 

validation_cats_dir = os.path.join(validation_dir, 'cats')
os.mkdir(validation_cats_dir)

# Directory with validation dog pictures

validation_dogs_dir = os.path.join(validation_dir, 'dogs')
os.mkdir(validation_dogs_dir)

# Directory with test cat pictures

test_cats_dir = os.path.join(test_dir, 'cats')
os.mkdir(test_cats_dir)

# Directory with test dog pictures

test_dogs_dir = os.path.join(test_dir, 'dogs')
os.mkdir(test_dogs_dir)

# Copying the first 1000 cat images to train_cats_dir

fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
    src = os.path.join(original_dir + 'train', fname)
    dst = os.path.join(train_cats_dir, fname)
    shutil.copyfile(src, dst)

1 个答案:

答案 0 :(得分:0)

所以我认为有两个问题:

  1. 首先使用Unix样式的斜杠('/'),但您使用的是Windows系统。因此,以后再调用os.path.join时,还会引入Windows样式斜杠('\\')。因此,在错误消息中会同时看到两种斜杠。

要解决此问题:使用os.path.join创建代表目录的所有字符串(见下文)

  1. 在倒数第三行src = os.path.join(original_dir + 'train', fname)中,您直接连接两个字符串,而不是用斜杠将它们连接在一起。这就是为什么您在错误消息中得到datasets/catsvdogstrain而不是预期的原因 datasets/catsvdogs/train

要解决此问题:通过os.path.join加入original_dir并“训练”(见下文)

这是修复代码的方法:

import os, shutil

# First, use os.path.join to reference your directories.
# That way you automatically pick the right "slash" (i.e. / or \\)
original_dir = os.path.join('datasets', 'catsvdogs')
base_dir = os.path.join('datasets', 'catsvdogs_small')

train_dir = os.path.join(base_dir, 'train')
os.mkdir(train_dir)

train_cats_dir = os.path.join(train_dir, 'cats')
os.mkdir(train_cats_dir)

# Copying the first 1000 cat images to train_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:

    # This will make a filepath that looks like 'datasets/catsvdogs/train'
    # You directly joined two strings via original_dir + 'train' which
    # gave you 'datasets/catsvdogstrain' in your error message
    src_dir = os.path.join(original_dir, 'train')

    src = os.path.join(src_dir, fname)
    dst = os.path.join(train_cats_dir, fname)

    shutil.copyfile(src, dst)