Table of Contents
File handling in python is vital with automation perspective.Files can be handled in python using many file operations,file modes,file methods and file attributes.
1. Write a Python program to read an entire text file.
2. Write a Python program to append text to a file and display the text.
3. Write a Python program to read last n lines of a file.
4. Write a Python program to show difference between ‘w’ mode and ‘a’ mode of file.
5. Write a Python program to calculate length of each line in file.
6. Write a Python program to read lines by applying loop on file object
7. Write a Python program to print specified characters from a file.
8. Write a Python program to print lines with ‘*’.
9. Write a Python program to show use of read(), readline() and readlines().
10. Write a Python program to show use of write(), writelines() and multiple write().
11. Write a Python program to determine file position using tell().
12. Write a Python program to get file position using seek().
Practical 6a : Write a Python program to read an entire text file.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to read an entire text file. """ def file_read(filename): #open() is used to open a file and to create a file object #txt is file object txt = open(filename) print(txt.read()) #read() is used to read entire data in file file_read('itvoyagers.txt') #text file must be created priorly
OUTPUT
>>> There are two types of files : i) Text Files ii) Binary Files Text Files: A text file is a structure containing sequence of lines and a line containing sequence of characters.A line is terminated by EOL (End OF Line) character.This type of file can hold alphabets, digits and symbols. Commonly used extensions : database(csv,..),source files(c,py,cpp,..), etc Binary files: Binary files can be any sequence of bytes and must be opened in an appropriate program that knows the specific file format. Commonly used extensions : images(jpg,gif,..), database(mdb,sqlite,..), executable(exe,dll,..) etc To know more about file handling visit:File Systems and File Handling in Python
NOTE : To get above output you must first create a text file named as itvoyagers.txt and write data into it
Practical 6b : Write a Python program to append text to a file and display the text.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to append text to a file and display the text. """ #'a' mode can create a file if it does not exist f=open('abc.txt','a') #write() is used to write text into file in both 'a' and 'w' modes f.write("easy to learnn") f=open('abc.txt','r') container=f.read() print(container) f.close()
OUTPUT
>>> >>> easy to learn easy to learn easy to learn easy to learn
NOTE : Program is executed 4 times to get above output
Practical 6c : Write a Python program to read last n lines of a file.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to read last n lines of a file. """ f=open('itvoyagers.txt','r') #readlines() is used to read entire file and returns list of lines list_of_lines=f.readlines() n=int(input("enter number of of lines")) print(list_of_lines[-n]) f.close() #close() performs clean up activities
OUTPUT
>>> enter number of of lines 8 Commonly used extensions : database(csv,..),source files(c,py,cpp,..), etc >>> ================================ RESTART ================================ >>> enter number of of lines 6 Binary files can be any sequence of bytes and must be opened in an appropriate program that knows the specific file format.
NOTE : itvoyagers.txt is already created in advance before using in this code
Practical 6d : Write a Python program to show difference between ‘w’ mode and ‘a’ mode of file.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to show difference between 'w' mode and 'a' mode of file. """ #difference between 'w' and 'a' """ 'w' mode is used to write in the file by overwriting existing content""" a=open("trial.txt","w") a.write("itv n") a.write("itv2 n") a=open("trial.txt","r") print(a.read()) """ 'a' mode is used to write in the file by adding content at the end without overwriting""" a=open("trial.txt","a") a.write("itv3 n") a=open("trial.txt","r") print(a.read())
OUTPUT
>>> itv itv2 itv itv2 itv3
NOTE : Above program explains difference between append and write mode
Practical 6e : Write a Python program to calculate length of each line in file.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to calculate length of each line in file. """ #prog to find length of each line of a file f=open("abc.txt","r") text=f.readlines() print(text) for line in text: print(len(line))
OUTPUT
>>> ['easy to learnn', 'easy to learnn', 'easy to learnn', 'easy to learnn'] 14 14 14 14
NOTE : As readlines() returns list of lines from file we can apply for loop on list and calculate length of each value.
Practical 6f : Write a Python program to read lines by applying loop on file object.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to read lines by applying loop on file object """ f=open("abc.txt","r") for line in f: print(line,end="")
OUTPUT
>>> easy to learn easy to learn easy to learn easy to learn
NOTE : file object holds entire file ,so we can apply loops on it to retrieve data
Practical 6g : Write a Python program to print specified characters from a file.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to print specified characters from a file. """ with open("abc.txt","r") as f: c=f.read(11) print(c)
OUTPUT
>>> easy to lea >>>
NOTE : We can provide n to read(n) to give number of characters to read.
Practical 6h : Write a Python program to print lines with ‘*’.
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to print lines with '*'. """ with open("abc.txt","r") as g: s=1 c=g.read(s) while len(c)>0: print(c,end="*") c=g.read(s)
OUTPUT
>>> e*a*s*y* *t*o* *l*e*a*r*n* *e*a*s*y* *t*o* *l*e*a*r*n* *e*a*s*y* *t*o* *l*e*a*r*n* *e*a*s*y* *t*o* *l*e*a*r*n* *
NOTE : Here we are seeing a different syntax to create file object and also the use of “end”
Practical 6i : Write a Python program to show use of read(), readline() and readlines().
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to show use of read(), readline() and readlines(). """ with open("abc.txt",'r') as f: c=f.read() print("OUTPUT OF read()") print(c) with open ("abc.txt") as f: c=f.readline() print("OUTPUT OF readline()") print(c) with open ("abc.txt") as f: c=f.readlines() print("OUTPUT OF readlines()") print(c)
OUTPUT
>>> OUTPUT OF read() easy to learn easy to learn easy to learn easy to learn OUTPUT OF readline() easy to learn OUTPUT OF readlines() ['easy to learnn', 'easy to learnn', 'easy to learnn', 'easy to learnn']
Practical 6j : Write a Python program to show use of write(), writelines() and multiple write().
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to show use of write(), writelines() and multiple write(). """ itv=open("file1.txt","w") itv.write("Python is easy") itv.write("n Python is interpreted language") lot=("n I","n love","n python") itv.writelines(lot) itv=open("file1.txt","r") c=itv.read() print(c)
OUTPUT
>>> Python is easy Python is interpreted language I love python
NOTE : Python does not have any writeline().
Practical 6k : Write a Python program to determine file position using tell().
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to determine file position using tell(). """ with open("abc.txt","r") as g: s=10 c=g.read(s) print("tell() returns : ",g.tell())
OUTPUT
>>> tell() returns 10 >>> python
Practical 6l : Write a Python program to get file position using seek().
""" Author : ITVoyagers (https://itvoyagers.in) Date :12th August 2019 Description : Write a Python program to get file position using seek(). """ with open("itvoyagers.txt","r") as f: s=10 c=f.read(s) print("output of read(s) when s=10",c,end='n') #seek(0,0) will take cursor to the beginning of file f.seek(0,0) c=f.read(s) print("output in returned with seek(0,0)",c) #seek(0,1) will take cursor to the current location in the file f.seek(0,1) c=f.read(s) print("output in returned with seek(0,1)",c) #seek(0,2) will take cursor to the end of file f.seek(0,2) c=f.read(s) print("output in returned with seek(0,2)",c)
OUTPUT
>>> output of read(s) when s=10 There are output in returned with seek(0,0) There are output in returned with seek(0,1) two types output in returned with seek(0,2)