TEXT FILE - Question Answers Q1 What is the difference between r+ and w+ mode? Ans r+ mode will return error if the does not exist while w+ mode will create a new file if file doesn’t exist. Q2 What is the difference between write and append mode? Ans write mode overwrites the existing data while append mode add the new data at the end of the file. Q3 Which method is used to close the file in python? Ans close() Q4 write the statement to close the file “test.txt” which is associated with file object named “fo”. Ans fo.close() Q5 write one similarity and one difference between a+ and w+. Ans similarity: in both the modes, we can do read and write opeations Difference: in w+ mode file will be truncated(previous data lost) while in a+ mode, file’s existing data will not be deleted and new data will be added at the end of the file. Q6 What is the difference between read() and read(n) functions? Ans read(): will read entire data from the file read(n) : will read first n characters/ bytes from the file. Q7 What is the difference between readline() and readlines()? Ans readline() : this function will read one line from the file readlines(): this function will read all the lines from the files. Q8 Write a program to read first 10 characters from a file names “data.txt” Ans f=open(“data.txt”,”r”) data=f.read(10) print(data) Q9 Write a program to read entire content from a file names “data.txt” Ans f=open(“data.txt”,”r”) data=f.read() print(data) Q10 Write the output of the following, if the data stored in file “data.txt” is given below.(“f” is the file object associated with file) try try but never cry print(f.read(4)) print(f.read(5)) print(f.read()) Ans try try b ut never cry #-------------------------------------------------------------------------------------------- Q1 Write a program in python to read entire content of file (data.txt) Ans f=open(“data.txt”,”r”) data=f.read() print(data) Q2 Write a program in python to read first 5 characters from the file (data.txt) Ans f=open(“data.txt”,”r”) data=f.read(5) print(data) Q3 Write a program in python to read first line from the file(data.txt) Ans f=open(“data.txt”,”r”) data=f.readline() print(data) Q4 Write a program in python to display number of lines in a file (data.txt) Ans f=open(“data.txt”,”r”) data=f.readlines() print(len(data)) Q5 Write a program in python to display first line from the file (data.txt) Ans f=open(“data.txt”,”r”) data=f.readlines() print(data[0]) Q6 readlines() function returns the entire data in the form of list of ____________ Ans list or string Q7 Name two functions which are used to write data into file. Ans write and writelines() Q8 Accept five names from the user and write in a file “data.txt” Ans f=open(“data.txt”,”w”) for i in range(5): n=input(“enter name”) f.write(n) f.close() Q9 Accept five names from the user and write in a file “data.txt”.(Each name should write in separate line) Ans f=open(“data.txt”,”w”) for i in range(5): n=input(“enter name”) f.write(n) f.write(‘\n’) f.close() Q10 Accept five names from the user and write in a file “data.txt”.(Each name should write in separate line) without using write() function Ans f=open(“data.txt”,”w”) h=[] for i in range(5): n=input(“enter name”) h.append(n,’\n’) f.writelines(h) f.close() #-------------------------------------------------------------------------------------------- Q1 Reading and writing operation can be done after close() function (T/F) Ans False Q2 f=open(“test.txt”,”r”) write the output of the following (a) print(f.name) (b) print(f.mode) (c) print(f.closed) (d) print(f.readable()) Ans (a) test.txt (b) r (c) False (d) True Q3 Write a program to accept five numbers from the user and write in a file “data.txt” Ans f=open(“data.txt,”w”) for i in range(5): n=int(input(“enter number”)) f.write(str(n)) f.close() Q4 What type of error is shown by the following line of code. f.write(3) where f is file handle associated with text file names “data.txt” Ans Typeerror Q5 write() function takes integer argument. (T/F) Ans False Q6 Write a program to accept 10 numbers from the user. If the number is even than write that number in “even.txt”, otherwise, write in file “odd.txt” Ans f=open(“even.txt”,”w”) f1=open(“odd.txt”,”w”) for i in range(10): n=int(input(“enter number”)) if(i%2==0): f.write(str(n)) f.write(‘\n’) else: f1.write(str(n)) f1.write(‘\n’) f.close() f1.close() Q7 Write a program to create a list of five subjects and write them in a file named “num.txt” Ans f=open(“num.txt”,”w”) s=[“English\n”,”Hindi\n”,”Math\n”,”Science\n”,”SST\n”] f.writelines(s) f.close() Q8 What type of error is shown in the following line of code? f.write(try) where f is file object and try is an object of list type Ans TypeError: write argument must be str, not list Q9 What type of error is shown in the following line of code? f.writelines(try) where f is file object and try is an object of list type Ans : AttributeError Q10 writelines() method does not add any EOL character to the end of string.(T/F) Ans True