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中“*”的作用

Python图像处理之简单画板实现方法

Python卸载不了怎么办

Python软件介绍

Python可以运行在jvm上吗

Python实现列表list以及list与数组array的相互转换的方法介绍

Python在普通工作中的应用有那些

Python: pandas如何高效运算的方法

Python中zip是什么函数

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




打赏

取消

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

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

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

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

评论

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