本文摘自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如何使用getpass库读取密码的方法介绍
怎么用Python
初学Python的学习心得
Python如何生成随机密码?
Python中的列表与元组有什么区别?一文搞懂元组与列表的异同点
Python中的os模块
Python空类型是什么
Python定制类__str__(实例详解)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python如何随机读取目录文件