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 7508275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:39:06+00:00 2026-05-29T22:39:06+00:00

I have a large image dataset (around 3000 files). My problem is simple, I

  • 0

I have a large image dataset (around 3000 files). My problem is simple, I want to copy randomly chosen image files to another destination. I use random.sample to select five hundred images and store their names in a list.
I now want to copy files from the src folder to a destination folder if their name exists in the list (and was hence randomly chosen).

The following code however copies ALL files in the folder, whether or not their names appear in the randomly selected list. help

import os.path
import os
import glob
import random
import shutil


dirfiles = os.listdir("/media/Data/Leaves/Leaves")
myfiles = []
myfiles.append(random.sample(dirfiles,500))


print myfiles


final_list=myfiles[0]
print final_list
count=0

for elem in final_list:
    print elem
    count= count+1

print count


src = '/home/mjanja/Desktop/Leaves'
dst = '/home/mjanja/Desktop/Positive Leaves'
for filename in final_list:
    for file in glob.glob( os.path.join(src,filename)):
        shutil.copy(file,dst)

print "Copied file!!" +infile
  • 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-05-29T22:39:07+00:00Added an answer on May 29, 2026 at 10:39 pm

    Your use of glob.glob in this case is where the danger occurs. That returns an iterator of all files that fit the pattern you provide. You’re building up a list of 500 specific files, but then matching by pattern … depending on what characters are in your filename this could give you extremely surprising results, as the pattern might very well match more files than your original 500.

    You are also doing some steps unnecessarily, and could wrap it all up in a function:

    import os
    import random
    import shutil
    
    def copy_sample(src, dst, size=500):
        files = [os.path.join(src, i) for i in random.sample(os.listdir(src), size)]
        count = len(files)
        for index, afile in enumerate(files):
            try:
               shutil.copy(afile, dst)
               print 'Copied file %s (%d/%d)' % (afile, index + 1, count)
            except Exception, msg:
               print 'Failed file %s (%d/%d) -- %s' % (afile, index + 1, count, msg)
    
    src = '/home/mjanja/Desktop/Leaves'
    dst = '/home/mjanja/Desktop/Positive Leaves'
    
    copy_sample(src, dst)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large image iI want to set as background for a div.
I have a large image and I want to make certain sections of the
I have a large image, size around 30000 (w) x 6000 (h) pixels. You
I have a large image and only want a rectangle portion of it to
I have a large image that contains a button graphic. I don't want the
I have a very large image I want to use as a CSS background.
I have a large non-map image that I want to allow people to view
I have a large image I want to resize while keeping the proportions
You have a large image saved in PNG format, its size lies around ~600KB.
Suppose I have a large image (5000 x 5000), how can I randomly select

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.