python随机读取目录文件的方法:
使用python模块: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")
登录后复制
以上就是python如何随机读取目录文件的详细内容,更多请关注Work网其它相关文章!