Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

File Handling Operation in Python:-


The file is a collection of records, if we want to store program output permanently under a computer harddisk then we can use file handling operation.

File handling provide multiple operations on a file


Python Provide a predefined method to manage file handling operation.

1 open():-  using this method we can open the file for operation, if the file not exist then it will create the file.

  f = open("filename",mode)    # w for write ,r for read ,w+ for write|read,r+ for read|write,a for append, a+ append and read

2 write():-  this method is used to write content into a file.

f.write(data)

code for write

f = open("student.txt","w")
f.write("student information")
f.close()

3 read()

code for read

f = open("student.txt","r")
s=f.read()
print(s)
f.close()


  s = f.read()
  print(s)



..................................................................................................................

4 append file:-

code for append

f = open("student.txt","a")
s = input("write content")
f.write(s)

f.close()

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,


Create Expenses System using File Handling Program:-

import time
f = open("expenses.txt","w")
expenseby = input("Enter expense by")
grocerry = input("Enter grocerry details")
milk = input("Enter milk expenses")
rent = input("Enter rent expenses")
date = time.asctime(time.localtime(time.time()))
f.write("Today Expenses Details")
f.write("\n")
f.write("Grocerry:"+grocerry)
f.write("\n")
f.write("Milk:" +milk)
f.write("\n")
f.write("Rent:" +rent)
f.write("\n")
f.write("Date:" + date)
f.close()



Expenses System program to append mode:-

import time
f = open("expenses.txt","a")
expenseby = input("Enter expense by")
grocerry = input("Enter grocerry details")
milk = input("Enter milk expenses")
rent = input("Enter rent expenses")
date = time.asctime(time.localtime(time.time()))
f.write("-------------*------------")
f.write("Today Expenses Details by "+expenseby)

f.write("\n")
f.write("Grocerry:"+grocerry)
f.write("\n")
f.write("Milk:" +milk)
f.write("\n")
f.write("Rent:" +rent)
f.write("\n")
f.write("Date:" + date)
f.write("-------------*------------")
f.close()



Expenses System Write and Read using Single File?

import time
f = open("expenses.txt","w+")
expenseby = input("Enter expense by")
grocerry = input("Enter grocerry details")
milk = input("Enter milk expenses")
rent = input("Enter rent expenses")
date = time.asctime(time.localtime(time.time()))
f.write("-------------*------------")
f.write("Today Expenses Details by "+expenseby)

f.write("\n")
f.write("Grocerry:"+grocerry)
f.write("\n")
f.write("Milk:" +milk)
f.write("\n")
f.write("Rent:" +rent)
f.write("\n")
f.write("Date:" + date)
f.write("-------------*------------")
f.seek(0)
s = f.read()
print(s)
f.close()




Expenses System Example With Append Mode:-

import time
f = open("expenses.txt","a+")
expenseby = input("Enter expense by")
grocerry = input("Enter grocerry details")
milk = input("Enter milk expenses")
rent = input("Enter rent expenses")
date = time.asctime(time.localtime(time.time()))
f.write("-------------*------------")
f.write("Today Expenses Details by "+expenseby)

f.write("\n")
f.write("Grocerry:"+grocerry)
f.write("\n")
f.write("Milk:" +milk)
f.write("\n")
f.write("Rent:" +rent)
f.write("\n")
f.write("Date:" + date)
f.write("-------------*------------")
f.seek(0)
s = f.read()
print(s)
f.close()










Program of file handling using class and object:-

class FileOperation:
    def createFile(self):
       self.f = open("info.txt","a+")
    def writeFile(self):
      title = input("Enter title")
      self.f.write("\n"+title)
      desc = input("Write content")
      self.f.write("\n"+desc)
    def readFile(self):
      self.f.seek(0)  #it will reset cursor position to any index
      res = self.f.read()
      print(res)
      self.f.close()

obj = FileOperation()
obj.createFile()
obj.writeFile()
obj.readFile()


Program to create a resume using file handling?

f = open("resume1.doc","w+")
f.write("Objective")
f.write("\n I am seeking job and i will work with full honesty and learn new skills")
f.write("\n Qualification")
f.write("\n I have completed BTECH from IPS college Indore")
f.write("\n Academic Qualification")
f.write("\n 10th 80% \n 12th 90% ")
f.close()



Program to calculate SI using P, R, and T?

p = float(input("enter amount"))
r = float(input("enter rate"))
t = float(input("enter time"))
si = (p*r*t)/100
f = open("sicalc.txt","w")
f.write("Amount is "+str(p)+ "\n Rate is "+str(r)+ "\n Time is "+str(t))
f.write("Result is "+str(si))
f.close()


Program to calculate Simple Interest using Write and Read Mode?

p = float(input("enter amount"))
r = float(input("enter rate"))
t = float(input("enter time"))
si = (p*r*t)/100
f = open("sicalc.txt","w+")
f.write("Amount is "+str(p)+ "\n Rate is "+str(r)+ "\n Time is "+str(t))
f.write("Result is "+str(si))
f.seek(0)
data = f.read()
print(data)
f.close()


Program to calculate Simple Interest using Append and Read Mode?

p = float(input("enter amount"))
r = float(input("enter rate"))
t = float(input("enter time"))
si = (p*r*t)/100
f = open("sicalc.txt","a+")
f.write("Amount is "+str(p)+ "\n Rate is "+str(r)+ "\n Time is "+str(t))
f.write("Result is "+str(si))
f.seek(0)
data = f.read()
print(data)
f.close()




Assignment:-

1)  WAP to perform the addition of two numbers and write the output of addition under file?


2)  WAP to create complete biodata for a marriage proposal?


3)  WAP to create horoscope software?










11 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. # WAP to perform the addition of two numbers and write the output of addition under file?
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    s=a+b
    f=open("addition.txt","w+")
    f.write("a is"+str(a)+"\n b is"+str(b)+"\nsum is"+str(s))
    f.close()

    ReplyDelete
  2. #wap to create bio data for marriag
    f = open("biodata.doc","w+")
    f.write("abc")
    f.write("\n In my family five member my parents and two sibling\n")
    f.write("\n i have complteted mbbs\n")
    f.write("\n age is 25\n")
    f.write("\n dob is 4-5-1995")
    f.write("\n hindu\n")
    f.write("\n working in govt college as a professor\n")
    f.write("\n i can speak hindi and english both\n")
    f.write("\n hindu\n")
    f.close()


    ReplyDelete
  3. #Program to create a resume using file handling?

    f = open("resume.doc","w+")
    f.write("Objective")
    f.write("\n I am seeking a job, where I can use my exp and skills for the growth of company")
    f.write("\n Qualification")
    f.write("\n I have completed B.E from Electronics and Communcation branch")
    f.write("\n Academic Qualification")
    f.write("\n 10th = 76% and 12th = 68%")
    f.seek(0)
    s=f.read()
    print(s)
    f.close()

    ReplyDelete
  4. #Program to calculate Simple Interest using Write and Read Mode?

    f=open("SI.txt","w+")
    p = int(input("Enter principal Amount"))
    r= int(input("Enter rate of interest = "))
    t = int(input("Enter time period = "))
    si = (p*r*t)/100
    f.write(str(si))
    f.seek(0)
    q=f.read()
    print("SI for ",p,r,t,"= ",q)
    f.close()

    ReplyDelete
  5. # Addition of two number
    a = int(input("Enter first number"))
    b = int(input("Enter second number"))
    ad =a +b

    f= open("Addition.txt","a+")
    f.write("\n"+str(ad))
    f.seek(0)
    q=f.read()
    print(str(a)+"+"+str(b)+"=" +str(ad))
    print(q)
    f.close()

    ReplyDelete
  6. # Biodata
    f= open("Biodata.doc","w+")
    f.write("Name - Parag")
    f.write("\n Occupation - Financial Advisor")
    f.write("\n DOB -26/11/1992")
    f.write("\n Manglik- No")
    f.write("\n Address- Indore")
    f.seek(0)
    q = f.read()
    print(q)
    f.close()

    ReplyDelete
  7. # Horoscope
    f=open("Horoscope.txt","w+")
    f.write("Aries- It is beat to not loosen the pursestrings till you become financially stable")
    f.write("\n Taurus - Money will come in as word about your skills gets around. taking break to go on a vaction is indicated")
    f.write("\n Cancer- Purchasing an expensive item can prove heavy on the pocket.")
    f.write("\n Leo- Agood understanding with spouse will bring happiness into your life.")
    f.write("\n Virgo- Homemakers will manage to reset the house")
    f.write("\n Libra- Paperwork regarding property is set to get completed soon")
    f.write("\n Scorpio - A professional advice taken from someone may not prove hundred percent correct")
    f.write("\n Sagittarius - Good preparation will find students performing well in a exam or competition")
    f.write("\n Capricon - You want to help out an elder by showing all your concern, but you can feel disappointed when it is not reciprocated.")
    f.write("\n Aquarius - You will managae to find time today to spend with family")
    f.write("\n Pisces - Family get-together will give you a chance to mingle with cousins and other relatives")
    f.seek(0)
    q= f.read()
    print(q)
    f.close()

    ReplyDelete
  8. #AKASH PATEL
    ADDITION OF TWO NUMBERS:----
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    sum=a+b
    f=open("addition.txt","w+")
    f.write("a is"+str(a)+"\n b is"+str(b)+"\n sum is"+str(sum))
    f.close()

    ReplyDelete
  9. #Akash patel
    f= open("Biodata.doc","w+")
    f.write("Name - Akash Patel ")
    f.write("\n Job- Cloud Engineer ")
    f.write("\n DOB -14/07/1998 ")
    f.write("\n Manglik- No")
    f.write("\n Address- Khargone ")
    f. write("\n Zodiac Sign-pisces")
    Bdata= f.read()
    print(Bdata)
    f.close()

    ReplyDelete
  10. WAP to create complete expenses sheet?

    import time
    while True:
    o = input("\n Press w to write \n r to read \n e to exit")
    if o=='w' or o=='W':
    f = open("expenses.txt","a")
    expenseby = input("Enter expense by")
    grocerry = input("Enter grocerry details")
    milk = input("Enter milk expenses")
    rent = input("Enter rent expenses")
    date = time.asctime(time.localtime(time.time()))
    f.write("\n Today Expenses Details")
    f.write("\n Expenses By " + expenseby)
    f.write("\n")
    f.write("Grocerry:"+grocerry)
    f.write("\n")
    f.write("Milk:" +milk)
    f.write("\n")
    f.write("Rent:" +rent)
    f.write("\n")
    f.write("Date:" + date)
    f.close()
    elif o=='r' or o=='R':
    f = open("expenses.txt","r")
    f.seek(0)
    data = f.read()
    print(data)
    elif o=='e' or o=='E':
    break

    ReplyDelete
  11. x = open("biodata.txt","a")
    DOB= input("Enter DOB")
    name = input("Enter name")
    caste = input("Enter caste")
    job = input("Enter JOB")
    gender=input("enter gender")
    x.write("\n biodata Details")
    x.write("\n date of birth " + DOB)
    x.write("\n")
    x.write("name:"+name)
    x.write("\n")
    x.write("caste:" +caste)
    x.write("\n")
    x.write("job:" +job)
    x.write("\n")
    x.write("gender:" + gender)
    x.close()
    x = open("biodata.txt","r")
    x.seek(0)
    data = x.read()
    print(data)

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post