[Edit: see final code below] I’m using the code below to randomly select 5 files from a source directory and then copy them to a new directory. It’s giving me an IO error in which it says “No such file or directory ‘x’ exists” where “x” is the filename without the directory path. Somehow it isn’t storing the path in “files.” I looked on this forum as well as a shutil tutorial but I can’t figure it out. (btw, this is similar to a previous question I asked but the code and error changed. I’ll post the final code to that question when I get it working). Thanks in advance!
import os
import shutil
import random
import os.path
src_dir = 'C:\\'
target_dir = 'C:\\Test'
src_files = (os.listdir(src_dir))
def valid_path(dir_path, filename):
full_path = os.path.join(dir_path, filename)
return os.path.isfile(full_path)
files = [f for f in src_files if valid_path(src_dir, f)]
choices = random.sample(files, 5)
for files in choices:
shutil.copyfile(files, target_dir)
print ('Finished!')
Try changing
shutil.copyfile(files, target_dir)to
shutil.copyfile(os.path.join(src_dir, files), target_dir)You’re only using the
src_dirto test whether the full path is valid, then you don’t use it again.Edit: Consider the following
Note that I changed
copyfiletocopysince you were specifying a destination directory and not an destination file.You could also add a leading
rbefore your strings to make them raw strings to avoid having to escape the backslash:But the SO syntax highlighter doesn’t like it so I took it out