Question

Problem 2 — Last 10 lines of a file Write a program named p2.py which takes...

Problem 2 — Last 10 lines of a file
Write a program named p2.py which takes the same file (or download it from the web)

/usr/share/doc/git/contrib/subtree/COPYING

and prints out the last 10 lines

Hint: you can use list() function

lines = list(fh)

to read in all the lines of the file as a list, or equivalently

lines = fh.readlines()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def read_last_lines(filename, no_of_lines=10):
   file = open(filename,'r')
   lines = file.readlines()
   last_lines = lines[-no_of_lines:]
   for line in last_lines:
       print(line)
   file.close()


if __name__ == "__main__":
   filename = "file.txt"
   read_last_lines(filename,2)

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)

Add a comment
Know the answer?
Add Answer to:
Problem 2 — Last 10 lines of a file Write a program named p2.py which takes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program named aIP.py which will read data from a file named wireShark.txt and...

    Write a Python program named aIP.py which will read data from a file named wireShark.txt and extract all the pairs of source and destination ip addresses and output them in pairs to another file called IPAddresses.txt , one per line, listing source and destination. Example of output: Source                      Destination 192.168.1.180         239.255.255.250 Detailed Requirements: You will read from a file called wireShark.txt which, to avoid problems with finding paths, will be located in the same directory as your code You will...

  • 2. Write a function file_copy() that takes two string parameters: in file and out_file and copies...

    2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...

  • Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a...

    Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processes the contents the file as follows: For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line. For file lines that begin with the letter 'u', print the upper case format for each word following the u. Sample...

  • PROBLEM 3 Rewrite Program 2. This is you must use keyword arguments to pass number of kWh and customer type to the bill_calculator function when it is called. Save your Python program in a file named Lab07P3.py. Submit the file to Blackboard for cred

    Problem 3  Rewrite Program 2.  This is you must use keyword arguments to pass number of kWh and customer type to the bill_calculator function when it is called. Save your Python program in a file named Lab07P3.py.  Submit the file to Blackboard for credit.This is what i wrote for prgram 2:():     kwh = (())     cus = (())     cus.upper()     bill_calculator(kwhcus) (kwhcus):     kwh <= cus == :         price = kwh * kwh > cus == :         price = * + ((kwh - ) * )     kwh <= cus == :         price = kwh * kwh > cus == :         price = * + ((kwh - ) * )     (.format(price))

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • 3) (10 marks) Write a C program named a5q3.c which removes adjacent duplicates from a list...

    3) (10 marks) Write a C program named a5q3.c which removes adjacent duplicates from a list of integers. The program reads the standard input expecting a list of integers separated by whites- pace. After reading each integer, it should print it on a separate line by itself, unless the integer is equal to the integer read before. This means that the program behaves in the same way as the Unix utility uniq but for integers instead of lines. You must...

  • 2. Write a program named lab3 2.app that contains a class named Student to store student...

    2. Write a program named lab3 2.app that contains a class named Student to store student information Information that will be relevant to store is first name, last name, id number, GPA, and major (1) Define a member function named set0, which allows you to set all member variables. This function has an empty parameter list, and its return type is void. So, within the function you'll prompt for information. Note: For entering major, if you need a space, use...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Write a program, which reads a list of student records from a file in batch mode....

    Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below. · SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel) The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e....

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT