Python は複数のフォルダーにアクセスし、要件を満たさないファイルを消去します

深層学習がビデオ データ セット内の短すぎるダーティ データを処理する場合、ネストされた複数レベルのフォルダーにバッチでアクセスし、短すぎるビデオ データを見つけて、これらのファイルを自動的に削除する必要があります。実装コードは次のとおりです。

import os
import cv2

# address of the files to process
rawpath = '/root/autodl-tmp/webvid/video10_20%'
video_len = 64

# to get dirs' path and filename
for root, dirs, files in os.walk(rawpath+'/'):
    for f in files:
#         a = root.rfind('/')
		# pt: path of a single file
        pt = root+'/'+f
        cap = cv2.VideoCapture(pt)
        flag = 0
        while flag < video_len:
            ret, img = cap.read()
            if ret:
                flag += 1
            else:
            	# just raise error or delete the chosen file
#                 raise NotImplementedError(f"the video '{pt}' is too short")
                print('rm -rf ' + pt)
                os.system('rm -rf ' + pt)
                break

おすすめ

転載: blog.csdn.net/m0_53327618/article/details/132788944
おすすめ