Considering A Real Time Application
Considering A Real Time Application
of os.listdir & random.choice methods
random_file=random.choice(os.listdir("Folder_Destination"))
Here's a sample python code which will move random files from one
directory to another
#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 : "))
Share
Improve this answer
Follow
THE_PHOENIX_777_TDW
611
1 silver badge
2
2 bronze badges
Add a comment
Share
Improve this answer
Follow
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
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.
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.
random_file=random.choice(os.listdir("Folder_Destination"))
Here's a sample python code which will move random files from one
directory to another
#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 : "))
Share
Improve this answer
Follow
THE_PHOENIX_777_TDW
611
1 silver badge
2
2 bronze badges
Add a comment
Share
Improve this answer
Follow
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
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.
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.