python如何随机读取目录文件

python如何随机读取目录文件,第1张

python如何随机读取目录文件

python随机读取目录文件的方法是使用python的模块【random argparse shutil】读取即可,其代码语句为【for x in os.listdir(path),if x.endswith('jpg')】。

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教程

以上就是python如何随机读取目录文件的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/682368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存