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

Question:

I am using the files from a video tutorial. At the beginning, it starts to spread the files of input image data by copying them in various folders. The code works in the tutorial but I wonder why I get the following error:

[Errno 22] Invalid argument: ‘D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggletraincat.1.jpg’

Here is the code. At first it creates the directories.(The catdogKaggletrain contains the input images):

import os, shutil
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = 'D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggletrain'

# The directory where we will
# store our smaller dataset
base_dir = 'D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKagglecatORdog'

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)

Then copies the images into the recently created folders:

# 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)

When I run this part, I get the following error:

OSError: [Errno 22] Invalid argument: ‘D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggletraincat.1.jpg’

Asked By: HoOman

||

Answers:

You are on Windows which is why you need to escape the backslashes or use raw strings to store file paths, i.e.:

original_dataset_dir = r'D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggletrain'

base_dir = r'D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKagglecatORdog'

or

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'
Answered By: Paul P

"t" character has a special meaning [TAB].
Either you double all the backslashes, to escape single slash.
Or you can use the raw strings like below.

original_dataset_dir = 'D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggletrain'
print(original_dataset_dir)

original_dataset_dir = r'D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggletrain'
print(original_dataset_dir) 

output:

D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggle rain
D:Machine LearningDeep LearningSRU-deeplearning-workshop-mastercatdogKaggletrain
Answered By: Marcel Preda
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.