0

複数のサブディレクトリに 170,000 以上の pickle ファイルのディレクトリがあり、これらは元々 (protocol=0) 形式を使用して pickle 化されていました。これは、時間的にも空間的にも非常に効率的ではありませんでした。

フォルダー内の各ファイルを (cPickle、プロトコル = 2 を使用して) 再ピクルするスクリプトを作成しましたが、奇妙なことに、スクリプトは特定のファイル (ファイル # 95,000) の処理中に例外をスローします。最初は、ピクルファイルが壊れていると思いました。この正確なピクル ファイルを IPython コマンド ラインから読み込もうとすると、ファイルは問題なく読み込まれます。

では、どうしてこうなったのか不思議です。これが私のスクリプトです。助けていただければ幸いです。

import os
import cPickle
import numpy
import time
import re
from progressbar import ProgressBar

inpath = '/path/to/folder'



def list_files(dir):                                                                                                
    r = []                                                                                                           
    subdirs = [x[0] for x in os.walk(dir)]                                                                            
    for subdir in subdirs:                                                                                            
        files = os.walk(subdir).next()[2]                                                                             
        if (len(files) > 0):                                                                                          
            for file in files:                                                                                        
                r.append(subdir + "/" + file)                                                                        
    return r     



infileList = list_files(inpath)
print "Total number of files found: %d" % len(infileList)
print "\n\n"

progress = ProgressBar()

outfilename = " "

print "Processing pickle files. Pls wait..."
t0 = time.time()
filecount = 0

for file in progress(infileList):
    try:
        arr = cPickle.load(open(file , "rb" ))
        outfilename = re.sub('/initial/path/','/new/path/',file)

        if not os.path.exists(os.path.dirname(outfilename)):
            os.makedirs(os.path.dirname(outfilename))
        with open(outfilename, "wb") as f:
            cPickle.dump(arr,f,protocol=2)

        filecount = filecount + 1


    except Exception,e:

        print "\n" + str(filecount)
        print "\nError occured while processing file: " +  outfilename 
        tx = time.time()
        print "\n Time elapsed: %.2f" % (tx-t0)
        continue

t1 = time.time()

total = t1-t0

print "Files repickled with protocol=2.\nRepickling execution time: %.2f sec" % total

4

0 に答える 0