I'm processing wav files and checking each for energy levels using python's wave module. Everything works great for dozens of files but then suddenly I start to get this MemoryError exceptions. For the meanwhile I ignore the exception and continue with the checks which sometimes fail with exception and sometimes gives me the answer.
The method that checks the files goes like this (Please focus at the MemoryError exception as my code works though it's not accessible to me at the moment so I've decided to rewrite it's basics):
import wave,os,glob
def wavCheck(filepath):
with open(filepath,'rb') as w:
handle = wave.open(w)
data = handle.readframes()
nframes = handle.getnframes()
channels = [[] for x in handle.getchannels()]
for i in nframes:
bucket = i%2
channels[bucket].append(data[i])
for channel in channels:
# calculate which frame is silent and do something
handle.close()
del handle
del nframes
del channels
return results
if __name__ == "__main__":
while os.path.exists(someDirectoryWhichContainWaves):
for filepath in glob.glob(someDirectoryWhichContainWaves+'\*.wav')
results = wavCheck(filepath)
# Do something with results
Well - assuming that reading the file is done ok and everything goes well and as expected and of course that the wave files are all ok, why would I get MemoryError exception?
Extra details -
- Wave files are between 3MBytes and 10MBytes
- I've been trying to customize Garbage Collector to collect every few iterations and this didn't work. After doing some reading about GC, I decided that the best way would be to check whether there is garbage to collect at all. So I did. This didn't work either.
Please- any ideas here?
Cheers.