I have this function that I want to read a single byte from a large file. The only problem is that after a certain amount of file reads the memory on the pc jumps up from a steady 1.5gb to 4gb and higher depending on how many file reads. (I break at 80 files because higher would crash my pc)
All i want is to get the 1 byte and not the whole file. Help please.
def mem_test():
count = 0
for dirpath, dnames, fnames in scandir.walk(restorePaths[0]):
for f in fnames:
if 'DIR.EDB' in f or 'PRIV.EDB' in f:
f = open(os.path.join(dirpath, f), 'rb')
f.read(1) #Problem line!
f.close()
if count > 80:
print 'EXIT'
sys.exit(1)
count += 1
mem_test()
The basic file operations in Python. This opens one file, reads the data into memory, then opens the second file and writes it out.
We can do this more succinctly by using the
with
keyboard to handle closing the file.If you don't want to store the entire file in memory, you can transfer it in pieces.