微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

[Errno 22] 无效参数:'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,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,dst)
    
# copy next 500 dog images to validation_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1000,fname)
    dst = os.path.join(validation_dogs_dir,dst)
    
# copy next 500 dog images to test_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1500,fname)
    dst = os.path.join(test_dogs_dir,dst)

当我运行这部分时,出现以下错误

OSError: [Errno 22] 无效参数: '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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。