python如何随机读取目录文件


本文摘自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安装jieba失败怎么办?

Python怎么读取数据

Python字符串与字典相关操作的详解

Python通过什么划分语句块

Python read lines() 有什么用?能用在什么地方?

Python中如何安装threading

Python如何在图片上添加文字

Python flask是什么

Python numpy 点数组去重

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...