Total= 35 marks Duration: 1Hour 30minutes Date: 26-07-2019
Chapter-03 File Handling in Python: 10 marks
Chapter-04 Python Libraries: 10 marks
Chapter-05 Recursion in Python: 10 marks
Chapter-06 Idea of Efficiency: 05 marks
Details:
Chapter-03 File Handling in Python: 10 marks
1. Program writing for the content of the file -2 marks
2. Program writing for the content of the file -2 marks
3. Output finding -3 marks
4. Program writing -3 marks
Chapter-04 Python Libraries: 10 marks
5. Output finding -3 marks
6. Direct question -2 marks
7. Writing program - 2 marks
8. Program writing through function - 3 marks
Chapter-05 Recursion in Python: 10 marks
9. Output finding for recursive program -2 marks 10. function writing through recursive: factorial/Fibonacci/binary search -4 marks 11. Output finding for recursive program -2 marks 12. Filling the blanks for written programming. -2 marks
Chapter-06 Idea of Efficiency: 05 marks
13. Efficiency calculating with the help of order. -3 marks
14. run time calculate efficiency -2 marks Library : It is a collection of modules A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Replace function
# Python3 program to demonstrate the
# use of replace()7 method
string ="geeks for geeks geeks geeks geeks"
# Prints the string by replacing geeks by Geeks
print(string.replace("geeks", "Geeks"))
# Prints the string by replacing only 3 occurence of Geeks
how to read last n lines from file using Python 3 programming language. The last n lines means, last 5 lines or last 10 lines etc. So n can be replaced by any positive integer value.
Generally we read file from the beginning but sometimes for our business requirements we may need to read a file from the end. So we may need to read file from end using any technology. Here I will show you how to read last n lines from file using Python.
We define a function called tail that takes three arguments – file name, number of lines to read from the end and block size in bytes.
The first argument, file name, is mandatory and others two are optional because we have provided default values for them.
import sys
import os
deffile_read_from_tail(fname,lines):
bufsize =8192
fsize = os.stat(fname).st_size
iter=0withopen(fname)as f:if bufsize > fsize:
bufsize = fsize-1
data =[]whileTrue:iter+=1
f.seek(fsize-bufsize*iter)
data.extend(f.readlines())iflen(data)>= lines or f.tell()==0:print(''.join(data[-lines:]))break
file_read_from_tail('test.txt',5)
In the above tail function, we use Python’s built-in seek function that has the following features:
seek() sets the file’s current position at the offset. The whence argument is optional and defaults to 0, which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file’s end. There is no return value.
The below line when calls the tail function prints the following output:
lines = tail("SampleTextFile_10kb.txt", 5)
for line in lines:
print (line)
Output:
In the above output we get five lines because we wanted to print last 5 lines.