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()

3.86
7
BonesVI 125 points

                                    $ split -l 10 data.json

3.86 (7 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source