I have a input file word.txt.I am trying to splitting the file in 75%-25% randomly in python.
def shuffle_split(infilename, outfilename1, outfilename2):
from random import shuffle
with open(infilename, 'r') as f:
lines = f.readlines()
# append a newline in case the last line didn't end with one
lines[-1] = lines[-1].rstrip('\n') + '\n'
traingdata = len(lines)* 75 // 100
testdata = len(lines)-traingdata
with open(outfilename1, 'w') as f:
f.writelines(lines[:traingdata])
with open(outfilename2, 'w') as f:
f.writelines(lines[:testdata])
But this code is writing the first 75% of the original file in the first output file and again the same 25% of the original file in the second output file.Could you please suggest me some way to solve it.