无法正确读取文件

时间:2018-10-09 20:20:50

标签: python

我正在尝试将数据集分为测试序列和验证。我正在读一本书,使用该书的语法时出现错误。我尝试用'\'甚至用'\'替换'/',但无法解决问题。 代码:

# --> Listing 5.4 Copying Images to training, validation and test direcotries
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)

# Copying the next 500 cat images to validation_cats_dir

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

# Copying the next 500 cat images to test_cats_dir

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

# Copying the first 1000 dog images to train_dogs_dir

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

# Copying the next 500 dog images to validation_dogs_dir

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

# Copying the next 500 dog images to test_dogs_dir 

fnames = ['dog.{}.jpg'.format(i) for i in range(1500, 2000)]
for fname in fnames:
    src = os.path.join(original_dir, fname)
    dst = os.path.join(test_dogs_dir, fname)
    shutil.copyfile(src, dst)


# Checking Images

print('Total train cat: ', len(os.listdir(train_cats_dir)))
print('Total train dog: ', len(os.listdir(train_dogs_dir)))
print('Total validation cat: ', len(os.listdir(validation_cats_dir)))
print('Total validation dog: ', len(os.listdir(validation_dogs_dir)))
print('Total test cat: ', len(os.listdir(test_cats_dir)))
print('Total test dog: ', len(os.listdir(test_dogs_dir)))

错误:

File "catsdogs.py", line 58, in <module>
    shutil.copyfile(src, dst)
  File "C:\Users\Leaf\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'datasets/catsready exists: 'datasets/vdogstrain\\cat.0.jpg'

我想知道为什么它使用'\'搜索图像。图像就在那里,我只是想不通如何使用os访问它们。从错误的外观来看,它停止在我的第一个for循环中,用于将部分图像移到另一个文件夹。

0 个答案:

没有答案