Python Programing Lab R16 EXERCISE - 7(a)

Python Programing Lab R16 EXERCISE - 7(a)


Aim:


      Write a program to print each line of a file in reverse order.


Description:


Traverse each line of the file and we have to display every line contents in reverse order. Here, We are using the some of the basic methods of files and a string method strip().

Python provides basic functions and methods necessary to manipulate files by default. We can do most of the file manipulations using a file object.

open():
Before we can read or write a file, we have to open it using Python‟s built-in open(). This function creates a file object, which would be utilized to call other support methods associated with it.

The Syntax for opening a file object in Python is:

file_object = open(“Name of the file”,”Mode of the file”)


close():
This method of a file object flushes any unwritten information and closes the file object. Python automatically closes a file when the reference object of a file is reassigned to another file.
The Syntax for closing a file object in Python is: 
file_object.close()
readlines():


This method will returns every line of the file as a list. The Syntax for readlines() in Python is:
           file_lines = file_object.readlines()
strip():

This method returns a copy of the string in which all chars have been stripped from the beginning and end of the string (default whitespace characters).
The Syntax for strip() is Python is: 
str.strip([chars])
Program:
f=open('netaji.txt','r').read()
#print(f)
print(f[::-1])
f=open('netaji.txt','r').readlines()
print(f)
print(f[::-1])

Output:



No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...