How to randomly read directory files in python

coldplay.xixi
Release: 2020-09-08 10:24:24
Original
4630 people have browsed it

The method for python to randomly read directory files is to use the python module [random argparse shutil] to read. The code statement is [for x in os.listdir(path),if x.endswith(' jpg')].

How to randomly read directory files in python

Python method of randomly reading directory files:

Use python module:random argparse shutil

import argparse parser = argparse.ArgumentParser() parser.add_argument('num',type=int,help="img numbers to random") args = parser.parse_args() import random import os path="/home/train/disk/data/yulan_park_expand" imgs = [] for x in os.listdir(path): if x.endswith('jpg'): imgs.append(x) selected_imgs=random.sample(imgs,k=args.num) print(selected_imgs) from shutil import copyfile for img in selected_imgs: src=os.path.join(path,img) dst=os.path.join(path,"../for_bitmain/"+img) copyfile(src,dst) print("copy done")
Copy after login

Related learning recommendations:python tutorial

The above is the detailed content of How to randomly read directory files in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!