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

  • Home
  • SEARCH
  • 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 7777889
In Process

The Archive Base Latest Questions

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

[Edit: see below for final code] I have the following code and I’m trying

  • 0

[Edit: see below for final code] I have the following code and I’m trying to figure out where to insert the random.choice code to make it select a single file, copy it, and repeat (here 6 times).

import os
import shutil
import random

dir_input = str(input("Enter Source Directory: "))
src_files = (os.listdir(dir_input))

for x in range (0,5):
    print ('This is the %d time' % x)
    for file_name in src_files:
        full_file_name = (os.path.join(dir_input, file_name))
        if (os.path.isfile(full_file_name)):
        print ('copying...' + full_file_name)
            shutil.copy(full_file_name, r'C:\Dir'))
else:
    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:15:27+00:00Added an answer on June 1, 2026 at 6:15 pm

    Thanks everyone for your help. The code changed significantly as I learned some things (and got help from people on this forum). The people on this site are quite awesome. Here’s the code:

    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 = [os.path.join(src_dir, f) for f in src_files if valid_path(src_dir, f)]
    choices = random.sample(files, 5)
    for files in choices:
        shutil.copy(files, target_dir)
    print ('Finished!')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: See my working code in the answers below. In brief: I have a
[Edit: see final code below] I'm using the code below to randomly select 5
I have a Controller with two Edit methods (see below). When I submit the
Edit: I have solved this by myself. See my answer below I have set
EDIT See my solution below /EDIT I have a Visual Studio solution with two
EDIT: See below unanswered part II. (ANSWERED) I. Appending URLS I am trying to
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
PLEASE SEE THE EDIT BELOW, THE REPORT SEEMS TO USE CACHED DATA? I cant
I receive this message (see image below) when I try to edit in debugging.
[EDIT: Problem solved. Please see my answer below.] In my app I call the

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.