本文摘自php中文网,作者coldplay.xixi,侵删。
python随机读取目录文件的方法是使用python的模块【random argparse shutil】读取即可,其代码语句为【for x in os.listdir(path),if x.endswith('jpg')】。

python随机读取目录文件的方法:
使用python模块:random argparse shutil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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如何随机读取目录文件的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python中构造方法的解析(附示例)
分享Python实现的二叉树定义与遍历
Python中flask_migrate,flask_script的使用介绍(附代码)
Python模块的编写与使用(实例解析)
Python 3.7新功能之dataclass装饰器详解
Python代码运行结果的显示
Python中next和send的用法介绍(代码)
Python学习有哪些网站
Python idle怎么用
Python3列表的基础学习(附示例)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python如何随机读取目录文件