0% found this document useful (0 votes)
33 views

Considering A Real Time Application

1) The code uses os.listdir and random.choice methods to randomly select a file from a specified folder. 2) os.listdir returns a list of file names in the given path, and random.choice selects a random element from that list. 3) The sample code then uses this method to randomly select and move files between directories as specified by the user.

Uploaded by

Ne vaznho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Considering A Real Time Application

1) The code uses os.listdir and random.choice methods to randomly select a file from a specified folder. 2) os.listdir returns a list of file names in the given path, and random.choice selects a random element from that list. 3) The sample code then uses this method to randomly select and move files between directories as specified by the user.

Uploaded by

Ne vaznho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

The simplest solution is to make use

of os.listdir & random.choice methods

random_file=random.choice(os.listdir("Folder_Destination"))

Let's take a look at it step by step :-

1} os.listdir method returns the list containing the name of entries (files)


in the path specified.

2} This list is then passed as a parameter to random.choice method


which returns a random file name from the list.

3} The file name is stored in random_file variable.

Considering a real time application

Here's a sample python code which will move random files from one
directory to another

import os, random, shutil

#Prompting user to enter number of files to select randomly along with directory
source=input("Enter the Source Directory : ")
dest=input("Enter the Destination Directory : ")
no_of_files=int(input("Enter The Number of Files To Select : "))

print("%"*25+"{ Details Of Transfer }"+"%"*25)


print("\n\nList of Files Moved to %s :-"%(dest))

#Using for loop to randomly choose multiple files


for i in range(no_of_files):
#Variable random_file stores the name of the random file chosen
random_file=random.choice(os.listdir(source))
print("%d} %s"%(i+1,random_file))
source_file="%s\%s"%(source,random_file)
dest_file=dest
#"shutil.move" function moves file from one directory to another
shutil.move(source_file,dest_file)

print("\n\n"+"$"*33+"[ Files Moved Successfully ]"+"$"*33)


You can check out the whole project on github Random File Picker

For addition reference about os.listdir & random.choice method you


can refer to tutorialspoint learn python

os.listdir :- Python listdir() method

random.choice :- Python choice() method

Share
Improve this answer
Follow

edited Oct 31 '18 at 4:57

answered Oct 1 '18 at 7:22

THE_PHOENIX_777_TDW
611
1 silver badge
2
2 bronze badges
Add a comment

Language agnostic solution:

1) Get the total no. of files in specified directory.

2) Pick a random number from 0 to [total no. of files - 1].


3) Get the list of filenames as a suitably indexed collection or such.

4) Pick the nth element, where n is the random number.

Share
Improve this answer
Follow

answered Mar 31 '09 at 15:04

karim79
328k64
64 gold badges
405
405 silver badges
402
402 bronze badges
• 1

Similarly language-agnostic: get the list of files in the directory, and pick one
by random. – Elazar May 7 '20 at 13:31


Add a comment

Independant from the language used, you can read all references to the
files in a directory into a datastructure like an array (something like
'listFiles'), get the length of the array. calculate a random number in the
range of '0' to 'arrayLength-1' and access the file at the certain index.
This should work, not only in python.

Share
Improve this answer
Follow

answered Mar 31 '09 at 15:01


Mork0075
5,7774
4 gold badges
22
22 silver badges
23
23 bronze badges
Add a comment

If you don't know before hand what files are there, you will need to get a
list, then just pick a random index in the list.

Here's one attempt:

import os
import random

def getRandomFile(path):
"""
Returns a random filename, chosen among the files of the given path.
"""
files = os.listdir(path)
index = random.randrange(0, len(files))
return files[index]
EDIT: The question now mentions a fear of a "race condition", which I
can only assume is the typical problem of files being added/removed
while you are in the process of trying to pick a random file.

I don't believe there is a way around that, other t

The simplest solution is to make use


of os.listdir & random.choice methods

random_file=random.choice(os.listdir("Folder_Destination"))

Let's take a look at it step by step :-


1} os.listdir method returns the list containing the name of entries (files)
in the path specified.

2} This list is then passed as a parameter to random.choice method


which returns a random file name from the list.

3} The file name is stored in random_file variable.

Considering a real time application

Here's a sample python code which will move random files from one
directory to another

import os, random, shutil

#Prompting user to enter number of files to select randomly along with directory
source=input("Enter the Source Directory : ")
dest=input("Enter the Destination Directory : ")
no_of_files=int(input("Enter The Number of Files To Select : "))

print("%"*25+"{ Details Of Transfer }"+"%"*25)


print("\n\nList of Files Moved to %s :-"%(dest))

#Using for loop to randomly choose multiple files


for i in range(no_of_files):
#Variable random_file stores the name of the random file chosen
random_file=random.choice(os.listdir(source))
print("%d} %s"%(i+1,random_file))
source_file="%s\%s"%(source,random_file)
dest_file=dest
#"shutil.move" function moves file from one directory to another
shutil.move(source_file,dest_file)

print("\n\n"+"$"*33+"[ Files Moved Successfully ]"+"$"*33)


You can check out the whole project on github Random File Picker
For addition reference about os.listdir & random.choice method you
can refer to tutorialspoint learn python

os.listdir :- Python listdir() method

random.choice :- Python choice() method

Share
Improve this answer
Follow

edited Oct 31 '18 at 4:57

answered Oct 1 '18 at 7:22

THE_PHOENIX_777_TDW
611
1 silver badge
2
2 bronze badges
Add a comment

Language agnostic solution:

1) Get the total no. of files in specified directory.

2) Pick a random number from 0 to [total no. of files - 1].

3) Get the list of filenames as a suitably indexed collection or such.

4) Pick the nth element, where n is the random number.

Share
Improve this answer
Follow

answered Mar 31 '09 at 15:04

karim79
328k64
64 gold badges
405
405 silver badges
402
402 bronze badges
• 1

Similarly language-agnostic: get the list of files in the directory, and pick one
by random. – Elazar May 7 '20 at 13:31


Add a comment

Independant from the language used, you can read all references to the
files in a directory into a datastructure like an array (something like
'listFiles'), get the length of the array. calculate a random number in the
range of '0' to 'arrayLength-1' and access the file at the certain index.
This should work, not only in python.

Share
Improve this answer
Follow

answered Mar 31 '09 at 15:01

Mork0075
5,7774
4 gold badges
22
22 silver badges
23
23 bronze badges
Add a comment

If you don't know before hand what files are there, you will need to get a
list, then just pick a random index in the list.

Here's one attempt:

import os
import random

def getRandomFile(path):
"""
Returns a random filename, chosen among the files of the given path.
"""
files = os.listdir(path)
index = random.randrange(0, len(files))
return files[index]
EDIT: The question now mentions a fear of a "race condition", which I
can only assume is the typical problem of files being added/removed
while you are in the process of trying to pick a random file.

I don't believe there is a way around that, other t

You might also like