[英]How to select file randomly from multiple sub-folders
I've multiple sub-folders and each sub-folder has multiple files.我有多个子文件夹,每个子文件夹都有多个文件。 I need to select the sub-folder randomly and then need to select a random file in that sub-folder.我需要随机 select 子文件夹,然后需要 select 该子文件夹中的随机文件。 Let's say I've five folders A, B, C, D, E, and each folder contains another folder named data and this data folder contains multiple files.假设我有五个文件夹 A、B、C、D、E,每个文件夹包含另一个名为 data 的文件夹,这个 data 文件夹包含多个文件。 I need to pick the folder randomly from the five folders and then open the data folder and finally randomly select a file.我需要从五个文件夹中随机选择文件夹然后打开data文件夹最后随机select一个文件。
Keep the folder names in a list.将文件夹名称保存在列表中。 import random import os folders = [0,1,2,3,4] selected_folder = random.choice(folders) path = selected_folder+"/data"
Now to take random file from the path, do random.choice() and pass the list of files in that path.现在要从路径中获取随机文件,请执行 random.choice() 并传递该路径中的文件列表。 Use os.listdir(path) to get the list of files.使用 os.listdir(path) 获取文件列表。
import os
import random
path = os.getcwd()
def getRandomFile(path):
randomDir = random.choice([(x) for x in list(os.scandir(path)) if x.is_dir()]).name
randomFile = random.choice([f for f in list(os.scandir(randomDir + "\\data\\"))]).name
return randomFile
print(getRandomFile(path))
Try this: (Python file must be in the same main folder as those 5 folders)试试这个:(Python 文件必须与这 5 个文件夹位于同一主文件夹中)
import os,random
lst=list(filter(lambda x: os.path.isdir(x), os.listdir('.'))) //get folder list
folder=random.choice(lst) //select random folder
os.chdir(os.path.join(os.path.dirname(__file__), folder, 'data')) // goto random folder/data
lst=list(filter(lambda x: os.path.isfile(x), os.listdir('.'))) //get file list
file=random.choice(lst) //get random file
print(file)
As I understand, you actually need 4 functions to build your block of code:据我了解,您实际上需要 4 个函数来构建代码块:
os.listdir(path)
which list all files and directories at a location os.listdir(path)
列出某个位置的所有文件和目录os.path.isdir(path)
to check if a element in a location is a directory os.path.isdir(path)
检查某个位置的元素是否为目录os.path.isfile(path)
idem with a files os.path.isfile(path)
与文件同上random.randrange(X)
to find a random number in the range [0; random.randrange(X)
在 [0; 范围内找到一个随机数; X[ X[I'm sure you can find easily the doc concerning those functions as they are all in the standard library of python.我相信您可以轻松找到有关这些函数的文档,因为它们都在 python 的标准库中。 Anyway here is your code:无论如何,这是您的代码:
import os
import random
path = "/home/johndoe/"
dirs = list(filter(lambda dir: os.path.isdir(os.path.join(path, dir)), os.listdir(path)))
dir_chosen = dirs[random.randrange(len(dirs))]
files_path = os.path.join(path, dir_chosen, "data")
files = list(filter(lambda file: os.path.isfile(os.path.join(files_path, file)), os.listdir(files_path)))
file_chosen = files[random.randrange(len(files))]
print("the file randomly chosen is: {}".format(os.path.join(files_path, file_chose )))
You can also check about os.path.join(a, b)
if you don't know about it but it is basically equivalent to a + '/' + b
on UNIX and a + '\' + b
on Windows.如果您不了解os.path.join(a, b)
,您也可以查看它,但它基本上相当于 UNIX 上a + '/' + b
和 Windows 上a + '\' + b
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.