File:- A file is a bunch of bytes stored on some storage
devices which can be read, write according to the
requirement.
Why there is need of file handling?
File help us to store the data permanently , which can
be retrieved for future use.
Types of files:-
  1. Text files
  2. Binary files
Text files:
  ● Stores information in ASCII or Unicode
    characters , so translation takes place.
  ● Each line of text terminated with a special
    character known as EOL(end of line)
  ● Extension for text file is .txt.
  ● Text file is default mode of file.
Binary files:-
  ● Contains information in the same format as it is
    stored in the memory.
  ● No translation takes place.
  ● There is no delimiter for a line i.e EOL.
  ● More secure.
  ● Extension for binary file is .dat
File Access Modes
Mode Description
r     Open file for reading only. This is the default mode.
w     Open file for writing only. Overwrites the file if
               already exists, else creates a new file.
a      Opens a file for appending. The file pointer is at
      the
               end of the file if the file exists. If file does not
      exist,
               it creates a new file for writing .
a+    Opens a file for both appending and reading.
r+    Opens a file for both reading and writing.
w+       Opens a file for both reading and writing.
rb    Open file for reading only in binary format. This is
               the default mode.
rb+      Opens a file for both reading and writing in
      binary
               format.
wb+      Opens a file for both reading and writing in
      binary
               format.
ab    Opens a file for appending in binary format.
ab+        Opens a file for appending and reading in binary
             format.
Data File Operations
  ● Opening a File
  ● Performing operations
  ● Closing a file
  Different types of operations can be:-
      ● Creating a file
      ● Displaying record from a file
      ● Appending data in a file
      ● searching data in a file
      ● Deleting data from a file
How to open a text file
FileObject=open<filename>
      Or
FileObject=open<filename>,<mode>
For example
Fobj=open(“text.txt”,w)
 Fobj=open(“c:\users\neena\n1.txt”,w)
 Fobj=open(r, ‘c:\users\neena\n1.txt’,w)
 File access modes
 By default mode is read
 How to write data in a file?
 There are two methods to write in a file:-
     ● write()
     ● writelines()
 Whenever we write in a file there are two possibilities
     1. File does not exist
     2. File already exists
 If file does not exist , it will create a new file.
 If file exists, it will overwrite the data in the file if it is in
 write mode
 Write() method
     ● It takes a string and write it in the file.
     ● For storing data with EOL character we have to
       add ‘\n’ character to the end of the string
     ● For storing numeric value , we have to either
       converted into string using str() or write in quotes.
     For example
#To create a file using write method
f=open("text2.txt","w")
f.write("hello\n")
f.write("how r u\n")
f.write("text file\n")
          Text file
Program file
  Open “text2.txt” in notepad
  When we remove”\n” from a file
#To create a file using write method
f=open("text2.txt","w")
f.write("hello")
f.write("how r u")
f.write("text file")
f.close()
output
    Writelines() method
       ● It used to write sequence data types in a file
         like(string, list and tuple etc).
       ● We have to give “\n”
#File creation using writeln method
f= open("list.txt","w")
list=["python\n","physics\n","chemistry\n","maths\n","english\n"]
f.writelines(list)
tuple=("83","44","46","45","301")
f.writelines(tuple)
f.close()
Output
                                    Value of tuple in
                                    single line because
                                    there is no “\n”
#TO ADD TWO NUMBERS AND STORE IN A FILE
f=open("sum.txt",'w')
num1=int(input("enter number1"))
num2=int(input("enter number2"))
sum=num1+num2
f.write("\nnum1= "+str(num1))
f.write("\tnum1= "+str(num1))
f.write("\tsum of two numbers= "+str(num1))
f.close()
output
num1= 34       num1= 34 sum of two numbers= 90
      Reading data from a file
      There are three methods to read from a file
      read()
          ● Reads at most n bytes. If no n is specified , reads
            the entire file.
          ● Returns the read bytes in the form of a string
          ● Syntax is:
          ● <fileobject>.read([n])
#TO read an entire file at a time
f=open("topic.txt",'r')
data=f.read()
print(data)
f.close()
output
>>>
   ============================================================= RESTART: C:/Users/Neena/
                           Desktop/notes/notes/file-handling/read.py
              ============================================================
File:- A file is a bunch of bytes stored on some storage devices
Why there is need of file handling?
File help us to store the data permanently , which can be retrieved for future use.
Types of files:-
1. Text files
2. Binary files
    To read specific number of bytes
#TO read specific number of bytes from a file
f=open("topic.txt",'r')
data=f.read(10)
print(data)
data=f.read(10)
print(data)
f.close()                 Original content of a file “topic.txt”
output                    File:- A file is a bunch of bytes stored on some storage
                          devices
File:- A f                Why there is need of file handling?
                          File help us to store the data permanently , which can be
ile is a b                retrieved for future use.
                          Types of files:-
                          1. Text files
                          2. Binary files
    Readline()
      ● Reads a line of input, if n is specified reads at
        most n bytes.
      ● Returns the read bytes in the form of a string
        ending with’\n’ character.
      ● Returns a blank string if no more bytes are left for
        reading in the file.
      ● Syntax:<fileobject>.readline()
#TO read a line form a file
f=open("topic.txt",'r')
data=f.readline()# read single line from a file
print(data)
f.close()
output
File:- A file is a bunch of bytes stored on some storage devices
#TO read number of characters form a line
f=open("topic.txt",'r')
data=f.readline(100)# read n number of characters from a line
print(data)
f.close()
output
File:- A file is a bunch of bytes stored on some storage devices
    Readlines()
  ● Reads all the lines of a text file.
  ● Returns in the form of list.
Syntax:-
<fileobject>.readlines()
#TO read all lines form a file
f=open("topic.txt",'r')
data=f.readlines()
print(data)
f.close()
output
['File:- A file is a bunch of bytes stored on some storage devices\n',
'Why there is need of file handling?\n',
 'File help us to store the data permanently , which can be retrieved
for future use.\n',
'Types of files:-\n', '1.\tText files\n',
'2.\tBinary files\n']