如果歸檔想法(這是順便說一句,最好的回答你的問題)不適合你,你可以融合在一個文件從多個文件中的數據,例如通過寫二進制數據的連續塊(從而創造一個未壓縮的檔案!)
令路徑是文件的列表應串連:
import io
import os
offsets = [] # the offsets that should be kept for later file navigation
last_offset = 0
fout = io.FileIO(out_path, 'w')
for path in paths:
f = io.FileIO(path) # stream IO
fout.write(f.read())
f.close()
last_offset += os.path.getsize(path)
offsets.append(last_offset)
fout.close()
# Pseudo: write the offsets to separate file e.g. by pickling
# ...
# reading the data, given that offsets[] list is available
file_ID = 10 # e.g. you need to read 10th file
f = io.FileIO(path)
f.seek(offsets[file_ID - 1]) # seek to required position
read_size = offsets[filed_ID] - offsets[file_ID - 1] # get the file size
data = f.read(read_size) # here we are!
f.close()
不,我不想使用arcihve文件。 – mehmet 2011-05-13 22:49:02
@mehmet,困惑 - 你想要什麼與檔案文件不同? – linuts 2011-05-13 23:08:03
我會使用zip壓縮tar,因爲它可以讓你直接訪問每個條目,而不必跳過前面的,這在處理聲音效果時很重要。 – 2011-05-14 14:13:34