(I believe I have been looking for hours on stackexchange's and the internet, but couldn't find the right answer)
What I'm trying to do here is to count the number of lines a file has, I achieved that with this code here
# Does not loud into memory
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f, 1):
pass
print(i)
file_len('bigdata.txt')
then I take the number of lines of the file and divide it by two/three/etc (to make two/three/etc files with the same amount of lines) e.g. bigdata.txt = 1000000 lines 1000000/2=500000 So here I will have two files with a 500000 lines in each, one starting from 1 to 500000 & the other from 500001 to 1000000. I already have this code which looks for a pattern in the original file(bigdata.txt), but I'm not looking for any pattern, just want to split the thing into two halfs or whatsover. Here is the code for it:
# Does not loud into memory
with open('bigdata.txt', 'r') as r:
with open('fhalf', 'w') as f:
for line in r:
if line == 'pattern\n': # Splits the file when there is an occurence of the pattern.
#But the occurence as you may notice won't be included in either the two files which is not a good thing since I need all the data.
break
f.write(line)
with open('shalf.txt', 'w') as f:
for line in r:
f.write(line)
So I'm looking for a simple solution and I know there is one, just can't figure it out for this moment. sample would be: file1.txt , file2.txt each with the same number lines give or take one. Thank you all for your time.