You can try this code:
file_input = open('mhyfile.txt','r')
for line in file_input:
#Your code here
The for line in file_input:
loop will read the file line by line. But I test in my linux system and find fileinput.input() use no more memory. I think you should give more information about your problem.
One possible problem is that you write too many files into your disk and cause the system crash. You can try to write the selected lines into one single file and mark the line number j
.