Python [Errno 22] 将文件从一个文件夹复制到另一个文件夹时参数无效

Python [Errno 22] Invalid argument when copy a file from one folder to another

我正在使用视频教程中的文件。一开始,它开始通过将输入图像数据的文件复制到各个文件夹中来传播它们。该代码在教程中有效,但我想知道为什么会出现以下错误:

[Errno 22] Invalid argument: 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train\cat.1.jpg'

这是代码。首先它创建目录。(catdogKaggle\train 包含输入图像):

import os, shutil
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'

# The directory where we will
# store our smaller dataset
base_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\catORdog'

os.mkdir(base_dir)

# Directories for our training,
# validation and test splits
train_dir = os.path.join(base_dir, 'train')
os.mkdir(train_dir)
validation_dir = os.path.join(base_dir, 'validation')
os.mkdir(validation_dir)
test_dir = os.path.join(base_dir, 'test')
os.mkdir(test_dir)

# Directory with our training cat pictures
train_cats_dir = os.path.join(train_dir, 'cats')
os.mkdir(train_cats_dir)

# Directory with our training dog pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')
os.mkdir(train_dogs_dir)

# Directory with our validation cat pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')
os.mkdir(validation_cats_dir)

# Directory with our validation dog pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
os.mkdir(validation_dogs_dir)

# Directory with our test cat pictures
test_cats_dir = os.path.join(test_dir, 'cats')
os.mkdir(test_cats_dir)

# Directory with our test dog pictures
test_dogs_dir = os.path.join(test_dir, 'dogs')
os.mkdir(test_dogs_dir)

然后将图像复制到最近创建的文件夹中:

# Copy first 1000 cat images to train_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1,1000)]
for fname in fnames:
    src = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(train_cats_dir, fname)
    shutil.copyfile(src, dst)

# Copy 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_dataset_dir, fname)
    dst = os.path.join(validation_cats_dir, fname)
    shutil.copy(src, dst)
    
# Copy 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_dataset_dir, fname)
    dst = os.path.join(test_cats_dir, fname)
    shutil.copyfile(src, dst)
    
# Copy 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_dataset_dir, fname)
    dst = os.path.join(train_dogs_dir, fname)
    shutil.copyfile(src, dst)
    
# Copy 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_dataset_dir, fname)
    dst = os.path.join(validation_dogs_dir, fname)
    shutil.copyfile(src, dst)
    
# Copy 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_dataset_dir, fname)
    dst = os.path.join(test_dogs_dir, fname)
    shutil.copyfile(src, dst)

当我运行这部分时,我得到以下错误:

OSError: [Errno 22] Invalid argument: 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train\cat.1.jpg'

你在 Windows 这就是为什么你需要转义反斜杠或使用 raw strings 来存储文件路径,即:

original_dataset_dir = r'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'

base_dir = r'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\catORdog'

original_dataset_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'

base_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\catORdog'

“\t”字符有特殊含义[TAB]。 要么将所有反斜杠加倍,以转义单斜杠。 或者您可以使用如下所示的原始字符串。

original_dataset_dir = 'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'
print(original_dataset_dir)

original_dataset_dir = r'D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train'
print(original_dataset_dir) 

输出:

D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle rain
D:\Machine Learning\Deep Learning\SRU-deeplearning-workshop-master\catdogKaggle\train