file handling in python class 12

file handling in python class 12

file handeling in python

It is used to store data or information on permanent storage device like hardisk.
 
file operation
1.creating file 
2.opening and exiting file 
3.read from file
4.writing to file
5.modifying file 
6.close file
 
text files                   binary file              operation
mode                          mode                      allowed
r                                    rb                             read only
w                                  wb                            write  only
a                                    ab                             append only
r+                                   rb+ or r+b               read and write
w+                                 wb+ or w+b            write and read
 a+                                  ab+ or  a+b              write and read

Types of files

Text files:-
 
1. extension of text file must be .txt
2. text file store information in index format (asscie or unicode)
3. read, write operation are slower
 
Binary file:-
 
1. extension of binary file can be anything except .txt commonly used .dat .
2. it store information in the same format in which the information is held in memory.
3. read, write operations faster.

practice questions

Q 1. write program to read a text file names.txt and calculate its size.

 
ans.   f = open(“d://names.txt”,”r”)
           str = f.read()
          size = len(str)
          print(“size of file=”,size,”bytes”)
 

Q 2. write program to create a text file capitals.txt and store n states and their capital in following format.

 
ans.  f = open(“d://capitals.txt”,”w”)
          n=int(input((“how much states?”))
          for i in range(1,n+1):
                 s=input(“enter state”)
                 c=input(“enter capital”)
                 p=s+”:”+c+”/n”
                 f.write(p)
          f.close
 

reading and writing to text files

 
1. f.read(n) – read ‘n’ bytes from file (1 char=1 byte).
2. f.read() – reads entire contents and store in string.
3. f.readline(n) – read n bytes.
4. f.readline() – read a single line.
5. f.readlines() – read all lines from files and stores in list.
6. f.write(string) – write given string to file.
7. f. writelines(list) – write given list to file.
 

Q 3.write a program to read first 25 character or 25 bytes of story.txt and display.

 
ans. f = open(“d://story.txt”,”r”)
         s = f.read(25)
         print(s)     
 

Q 4. reading entire file in one go.

 
 ans. f = open(“d://story.txt”,”r”)
          s =   f.read()
          print(s)
 

Q 5. for reading 5 lines.

 
ans. s = f.readline(5)
        print(s)
 

 

Q 6. for writing line.

 
ans. f= open(“d://mystory.txt”,”w”)
        f.write(5)
        f.close
 

 standard input/output and error streams

1. stdin:- standard input from steam.
2. stdout:-standard output to output stream.
3. stderr:-standard error to display error.
 

with statement:-will automatically close the file after nested block of code.

ex.    with open(“filexyz.txt”,”w”) as f:
          :
           file operations
          :
        f.close()     X it is not require X
 

Q 7. write a program to read a text file data.txt and print only numbers or digits from the file.

 
ans. f=open(“d://data.txt”,”r”)
         for line in f :
             l=len(line)
             i=0
            while i<l:
                    if line [i]>=48 and line[i]<=57:
                           print(line[i])
                           i=i+1
        f.close()              
    

Q 8. write a program to print only the last line of a text file.

 
ans. f = open(“story.txt”,”r”)
         l = f.readlines()
         print(l[-1])
        f.close()                                       

file handling in python class 12 important questions