1

[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!')
  • Aside: range(0,5) doesn't give you 6 times, it only gives you five. range doesn't include the upper bound. – DSM Mar 06 '12 at 17:49

4 Answers4

3

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!')
1

Your code copies all the files from one directory to 'C:\Dir' every loop. That doesn't seem to be what you want. Also, you said a single file. Your code will just dump the list of everything in the entered directory. You might also want to consider using raw_input instead of input. Here's what I recommend:

import os
import shutil
import random

# let's seed the random number, it's at least good practice :-)
random.seed()
dir_input = raw_input("Enter Source Directory: ")
# let's get ONLY the files in this directory
src_files = [ f for f in os.listdir(dir_input) if os.path.isfile(os.path.join(dir_input,f))]
for x in range (0,5):
  print ('This is the %d time' % x)
  # I'll let you copy this where you want, but this will
  # choose a random file once per loop
  print random.choice(src_files)

Let me know if I misunderstood.

macduff
  • 4,655
  • 18
  • 29
  • Thanks for this. No, it's not homework. I wish it was! I'm a grad student in sociology and I'm teaching myself Python out of books so I can manipulate 250k txt files for a database. And because I'm so new I have to actually figure out exactly what this is doing first. – user1252778 Mar 06 '12 at 23:46
  • I'm impressed at your initiative! Wish you the best in your studies! – macduff Mar 07 '12 at 00:02
1

Filter the paths first so you get a list that contains only valid choices:

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(dir_input, f)]

Then, if you want n unique files, you can use random.sample():

choices = random.sample(files, n)

Or if you rather want to allow multiple instances of the same file:

choices = [random.choice(files) for i in range(n)]
Dan Gerhardsson
  • 1,869
  • 13
  • 12
  • Thanks, this is mostly working. I have an error that says "No such file or directory" with a drive letter that changes. I think it's looking locally for some reason. I'll post the solution when I get it to work. I appreciate the help. – user1252778 Mar 06 '12 at 23:48
  • @user1252778: You're welcome. Post your updated code and/or detailed error messages if you need more help. – Dan Gerhardsson Mar 07 '12 at 17:48
0

Dis-indent the last statement, otherwise you'll see the message 5 times during the copying and not after it.

user1016274
  • 4,071
  • 1
  • 23
  • 19