简体   繁体   English

如何从多个子文件夹中随机选择 select 文件

[英]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.

相关问题 如何在不指定子文件夹名称的情况下自动合并来自多个子文件夹的多个 CSV? - How to automatically merge multiple CSVs from multiple sub-folders, without specifying sub-folder name? 如何从文件夹和子文件夹中保存音频文件 (.wav) 的频谱图? - How can i save spectrogram of audio file (.wav) from folders and sub-folders? 从一系列子文件夹中获取最后写入的文件 - Get the last written file from the series of the sub-folders 如何将特定文件与子文件夹分开? - How to separate specific files from sub-folders? 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 如何让Python查看子文件夹? - How to get Python to look at Sub-Folders? 如何使用子文件夹模板在 cookiecutter 模板中创建多个子文件夹 - How to create multiple sub-folders in cookiecutter template using a sub-folder template 训练具有多个文件夹和子文件夹的 CNN 模型 - Train CNN model with multiple folders and sub-folders 检索 Python / RobotFramework 文件夹/子文件夹中的所有文件名 - Retrieve all file names in folders/sub-folders in Python / RobotFramework 将批处理图像文件从子文件夹复制到父文件夹 - Copying batch image files from sub-folders to parent folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM