Computer Science
Write a function reverseFile() in Python to read lines from text file 'TESTFILE.TXT' and display the file content in reverse order so that the last line is displayed first and the first line is displayed at the end.
Python
Python File Handling
3 Likes
Answer
def reverseFile():
file = open('TESTFILE.TXT', 'r')
lines = file.readlines()
line_count = len(lines)
for i in range(line_count - 1, -1, -1):
print(lines[i].strip())
reverseFile()Output
A marked difference will come in our country.
We all pray for everyone's safety
An apple a day keeps the doctor away.
Answered By
1 Like
Related Questions
In a database BANK, there are two tables with a sample data given below:
Table: EMPLOYEE
ENo EName Salary Zone Age Grade Dept 1 Mona 70000 East 40 A 10 2 Mukhtar 71000 West 45 B 20 3 Nalini 60000 East 26 A 10 4 Sanaj 65000 South 36 A 20 5 Surya 58000 North 30 B 30 Table: DEPARTMENT
Dept DName HOD 10 Computers 1 20 Economics 2 30 English 5 (a) To display ENo, EName, Salary and corresponding DName of all the employees whose age is between 25 and 35 (both values inclusive).
(b) To display DName and corresponding EName from the tables DEPARTMENT and EMPLOYEE (Hint: HOD of DEPARTMENT table should be matched with ENo of EMPLOYEE table for getting the desired result).
(c) To display EName, Salary, Zone and Income Tax (Note: Income tax to be calculated as 30% of salary) of all the employees with appropriate column headings.
Write a function begEnd() in Python to read lines from text file 'TESTFILE.TXT' and display the first and the last character of every line of the file (ignoring the leading and trailing white space characters).
Example: If the file content is as follows:
An apple a day keeps the doctor away. We all pray for everyone's safety A marked difference will come in our country.Then begEnd () function should display the output as:
A. Wy A.Consider the following table STUDENT.
NO NAME AGE DEPARTMENT FEE SEX 1 PANKAJ 24 COMPUTER 120 M 2 SHALINI 21 HISTORY 200 F 3 SANJAY 22 HINDI 300 M 4 SUDHA 25 HISTORY 400 F Write a Python code to search a record as per given NO (number) using MySQL connectivity and print the data.
Write a Python code to insert a new record as per given table Student (No, Name, Age, Department, Fee, Sex) using MySQL connectivity.