How to pickle several .txt files into one pickle

后端 未结 2 904
既然无缘
既然无缘 2021-01-26 23:37

I need to overcome some cPickle constrains, namely i need to open several files and pickle them to one file, like this:

import cPickle

file1=open(\'file1.txt\',         


        
相关标签:
2条回答
  • 2021-01-26 23:43

    This is the correct way, it pickles the file contents:

    file1=open('file1.txt','r')
    file2=open('file2.txt','r')
    obj=[file1.read(), file2.read()]
    cPickle.dump(obj,open('result.i2','w'),2)
    

    If you set obj=[file1,file2] you are not pickling the file contents, you are pickling the file handles.

    0 讨论(0)
  • 2021-01-26 23:49

    Proper code to save multiple .txt files into one, and then unpack them into text files again, based on Dietrich Epp 's answer:

    import cPickle,os
    
    
    def save(dir):
        result_file=open(dir+'/result.i2','wb')        
        list=os.listdir(dir) 
        obj=[list]   
        for file in list:
            print file
            f=open(dir+"/"+file,'rb')        
            obj.append(f.read())
    
        cPickle.dump(obj,result_file,2)
    
    def load(path):
        f=open(path+"/"+'result.i2','rb')
    
    
        obj=cPickle.load(f)    
        for i in range(1,len(obj)):
            file=open(path+"/"+obj[0][i-1],'wb')
            file.writelines(obj[i])
            file.close()
    
    0 讨论(0)
提交回复
热议问题