首頁  >  文章  >  後端開發  >  python如何隨機讀取目錄文件

python如何隨機讀取目錄文件

coldplay.xixi
coldplay.xixi原創
2020-09-03 10:14:384654瀏覽

python隨機讀取目錄檔案的方法是使用python的模組【random argparse shutil】讀取即可,其程式碼語句為【for x in os.listdir(path),if x.endswith(' jpg')】。

python如何隨機讀取目錄文件

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如何隨機讀取目錄文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:關於Pyzmq介紹下一篇:關於Pyzmq介紹