split file into multiple files code example
Example 1: split one file into multiple files
##Write a Python program to create a file of numbers by taking input from the user. Split this file into two files where one file contains odd numbers and the other file contains even numbers from the file. You can take input of non-zero numbers, with an appropriate prompt, from the user until the user enters a zero to create the file assuming that the numbers are non-zero.
f = open('NumFile.txt','w')
while True :
no = int(input("enter a number (0 for exit)"))
if no == 0 :
print("\n!!!!!!!!!!! EXIT !!!!!!!!!!!!")
break
else :
f.write(str(no) + "\n")
f.close()
#read data from input file and write into even odd file
with open ('NumFile.txt') as f1, open ('FileOdd.txt','w') as fo, open ('FileEven.txt','w') as fe :
for i in f1:
if int(i) % 2 == 0 :
fe.write(i+"\n")
else :
fo.write(i+"\n")
# read data from even and odd file
f1 = open ('FileOdd.txt','r')
f2 = open ('FileEven.txt','r')
print("\nContent of Even number file :: \n",f2.read().strip())
print("\nContent of Odd number file :: \n",f1.read().strip())
f1.close()
f2.close()
Example 2: split large file into smaller files
$ split -l 10 data.json