Generation

generate functionSun, 15 Jan 2023

read file and split the lines into 7 separate files

def split_to_7_files(filename): # read file f = open(filename, "r") lines = f.readlines() f.close() # split the lines into 7 separate files n = len(lines) k = n // 7 for i in range(7): # create output filename fout_name = filename.split(".")[0] + "_" + str(i) + "." + filename.split(".")[1] # write to file fout = open(fout_name, "w") for j in range(i*k, (i+1)*k): fout.write(lines[j]) fout.close() split_to_7_files("text.txt")

Questions about programming?Chat with your personal AI assistant