Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7777059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:02:38+00:00 2026-06-01T18:02:38+00:00

[Edit: see final code below] I’m using the code below to randomly select 5

  • 0

[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!')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:02:39+00:00Added an answer on June 1, 2026 at 6:02 pm

    Try changing

    shutil.copyfile(files, target_dir)

    to

    shutil.copyfile(os.path.join(src_dir, files), target_dir)

    You’re only using the src_dir to test whether the full path is valid, then you don’t use it again.

    Edit: Consider the following

    src_dir = 'C:\\'
    target_dir = 'C:\\Test'
    # Create a list of filenames
    src_files = (os.listdir(src_dir)) 
    # Create a list of full paths (valid)
    src_paths = [f for f in [os.path.join(src_dir, f) for f in src_files] if os.path.isfile(f)]
    # Do selection
    choices = random.sample(src_paths, 5)
    for path in choices:
        print path
        shutil.copy(path, target_dir)
        print ('Finished!')
    

    Note that I changed copyfile to copy since you were specifying a destination directory and not an destination file.

    You could also add a leading r before your strings to make them raw strings to avoid having to escape the backslash:

    dir = 'C:\\Temp\\' # Is the same as
    dir = r'C:\Temp\'
    

    But the SO syntax highlighter doesn’t like it so I took it out

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Edit: see below for final code] I have the following code and I'm trying
EDIT: See my working code in the answers below. In brief: I have a
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
I have a Controller with two Edit methods (see below). When I submit the
PLEASE SEE THE EDIT BELOW, THE REPORT SEEMS TO USE CACHED DATA? I cant
[EDIT: Problem solved. Please see my answer below.] In my app I call the
Edit: I have solved this by myself. See my answer below I have set
Final Edit : The wall of text below can be summed up by simply
FINAL EDIT: It does indeed appear to be a compiler bug - see the
I see this [below] all over in the Android code (and some other code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.