136 solutions available
Question 1Assertion (A): The file in Python is used to store information on a named location in the secondary storage device.Reasoning (R): In...
Question 2Assertion (A): The file access mode used to add or append the data in the file is 'a'.Reasoning (R): In the access mode, 'a', the text will...
Question 3Assertion (A): The binary files are an advanced version of text files and work similar to text files.Reasoning (R): The data in binary...
Question 4Assertion (A): To work with binary files, we need to import pickle module.Reasoning (R): Pickle module contains two important methods for...
Question 5Assertion (A): The close() method is used to close the file.Reasoning (R): While closing a file, the system frees up all the resources like...
Question 6Assertion (A): The readline() method reads one complete line from a text file.Reasoning (R): The readline() function can also be used to...
Question 7Assertion (A): CSV stands for Comma Separated Values.Reasoning (R): CSV files are common file format for transferring and storing data.Both...
Question 8Assertion (A): CSV (Comma Separated Values) is a file format for data storage which looks like a text file.Reasoning (R): In CSV file, the...
Question 9Assertion (A): Text file stores information in the ASCII or Unicode format/characters.Reasoning (R): In a text file, there is no delimiter...
Question 1Files in Python are interpreted as a sequence or stream of bytes stored on some storage media.
Question 2The open() function creates a file object used to call other support methods associated with it.
Question 3Files in Python can be opened in one of the three modes — read ('r'), write('w') and append('a').
Question 4The writelines() method writes a list of strings to a file.
Question 5The close() method of a file object flushes any unwritten information and closes the file object.
Question 6The name of the current working directory can be determined using getcwd() method.
Question 7The rename() method is used to rename the file or folder.
Question 8The remove() method is used to remove/delete a file.
Question 9A binary file is a series of 1's and 0's, treated as raw data and read byte-by-byte.
Question 10The with statement automatically closes the file after the processing on the file gets over.
Question 11The read() function reads data from the beginning of a file.
Question 12The pickle module produces two main methods called dump() and load() for writing and reading operations.
Question 13The readlines() returns a list of strings from the file till end of file (EOF).
Question 14The read(n) method reads 'n' characters from the file.
Question 15flush() function is used to force transfer of data from buffer to file.
Question 16CSV format is a text format accessible to all applications across several platforms.
Question 17seek() method is used for random access of data in a CSV file.
Question 18dump() method of pickle module is used to write an object into binary file.
Question 19load() method of pickle module is used to read data from a binary file.
Question 20import csv statement is given for importing csv module into your program.
Question 21join() is a string method that joins the string with a string separator.
Question 22line_num object contains the number of the current line in a CSV file.
Question 23To read all the file contents in the form of a list, readlines() method is used.
Question 24To write contents of list, tuple and sequence data type writelines() method may be used.
Question 25To force Python to write the contents of file buffer on to storage file, flush() method may be used.
Question 26When we work with file, buffer area is automatically associated with the file when we open it.
Question 1To open a file c:\test.txt for reading, we should give the statement:file1= open("c:\ test.txt", "r")file1 = open("c:\\ test.txt",...
Question 2To open a file c:\test.txt for writing, we should use the statement:fobj = open("c:\test.txt", "w")fobj = open("c:\\test.txt", "w")fobj =...
Question 3To open a file c:\test.txt for appending data, we can give the statement:fobj = open("c:\\test.txt", "a")fobj = open("c:\\test.txt",...
Question 4Which of the following statements is/are true?When we open a file for reading, if the file does not exist, an error occurs.When we open a...
Question 5To read two characters from a file object fobj, the command should be:fobj.read(2)fobj.read()fobj.readline()fobj.readlines()
Question 6To read the entire contents of the file as a string from a file object fobj, the command should...
Question 7What will be the output of the following snippet?f = None for i in range(5): with open("data.txt", "w") as f: if i > 2:...
Question 8To read the next line of the file from a file object fobj, we use:fobj.read(2)fobj.read()fobj.readline()fobj.readlines()
Question 9To read the remaining lines of the file from a file object fobj, we use:fobj.read(2)fobj.read()fobj readline()fobj.readlines()
Question 10The readlines() method returns:StringA list of integersA list of single charactersA list of strings
Question 11Which module is required to use the built-in function dump()?mathflushpickleunpickle
Question 12Which of the following functions is used to write data in the binary mode?writeoutputdumpsend
Question 13Which is/are the basic I/O (input-output) stream(s) in file?Standard InputStandard OutputStandard ErrorsAll of these
Question 14Which of the following is the correct syntax of file object 'fobj' to write sequence data type using writelines()...
Question 15In file handling, what do the terms "r" and "a" stand for?read, appendappend, readwrite, appendNone of these
Question 16Which of the following is not a valid mode to open a file?abrwr+w+
Question 17Which statement is used to change the file position to an offset value from the start?fp.seek(offset, 0)fp.seek(offset, 1)fp.seek(offset,...
Question 18The difference between r+ and w+ modes is expressed as?No differenceIn r+ mode, the pointer is initially placed at the beginning of the...
Question 19What does CSV stand for?Cursor Separated VariablesComma Separated ValuesCursor Separated ValuesCursor Separated Version
Question 20Which module is used for working with CSV files in Python?randomstatisticscsvmath
Question 21Which of the following modes is used for both writing and reading from a binary file?wb+wwbw+
Question 22Which statement is used to retrieve the current position within the file?fp.seek()fp.tell()fp.locfp.pos
Question 23What happens if no arguments are passed to the seek() method?file position is set to the start of filefile position is set to the end of...
Question 24Which of the following modes will refer to binary data?rw+b
Question 25Every record in a CSV file is stored in reader object in the form of a list using which method?writer()append()reader()list()
Question 26For storing numeric data in a text file, it needs to be converted into ............... using ............... function.integer,...
Question 1What is the difference between "w" and "a" modes?
Question 2What is the significance of file-object?
Question 3How are open() functions different from close() functions other than their functionality of opening and closing files?
Question 4Write statements to open a binary file 'C:\Myfiles\text1.dat' in read and write mode by specifying the file path in two different formats.
Question 5In which of the following file modes will the existing data of the file not be lost?rbabww + ba + bwbwb+w+r+
Question 6What would be the data type of variable data in the following statements?data = f.read()data = f.read(10)data = f.readline()data =...
Question 7When a file is opened for output in write mode, what happens when(a) the mentioned file does not exist?(b) the mentioned file exists?
Question 8What role is played by file modes in file operations? Describe the various file mode constants and their meanings.
Question 9Write a code snippet that will create an object called fileout for writing; associate it with the filename 'STRS'. The code should keep on...
Question 10Explain how many file objects would you need to create to manage the following situations:(a) To process three files sequentially.(b) To...
Question 11Write the advantages of saving data in:(a) Binary form(b) Text form
Question 12Why is it required to close a file after performing the reading and writing operations on a file?
Question 13When do you think text files should be preferred over binary files?
Question 14Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces...
Question 15A file 'sports.dat' contains information in the following format: EventName, ParticipantWrite a function that would read contents from...
Question 16Write a program to count the words "to" and "the" present in a text file "Poem.txt".
Question 17Write a program to count the number of uppercase alphabets present in a text file "Poem.txt".
Question 18Write a program that copies one file to another. Have the program read the file names from user.
Question 19Write a program that appends the contents of one file to another. Have the program take the file names from the user.
Question 20Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file 'LOWER', all...
Question 21Write a program to search the names and addresses of persons having age more than 30 in the data list of persons stored in a text file.
Question 22Write a function in Python to count and display the number of lines starting with alphabet 'A' present in a text file "LINES.TXT", e.g.,...
Question 23Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files:a...
Question 24Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it? Will some...
Question 25What is the difference between the following sets of statements (a) and (b):(a)P = open("practice.txt", "r") P.read(10)(b)with...
Question 26Write a program to accept string/sentences from the user till the user enters "END". Save the data in a text file and then display only...
Question 27Write a function to insert a sentence in a text file, assuming that text file is very big and can't fit in computer's memory.
Question 28Write a program to read a file 'Story.txt' and create another file, storing an index of 'Story.txt', telling which line of the file each...
Question 29Write a program to accept a filename from the user and display all the lines from the file which contain Python comment character '#'.
Question 30Reading a file line by line from the beginning is a common task. What if you want to read a file backward ? This happens when you need to...
Question 31Write a Python program to display the size of a file after removing EOL characters, leading and trailing white spaces and blank lines.
Question 32Write a function Remove_Lowercase() that accepts two file names, and copies all lines that do not start with lowercase letter from the...
Question 33Write a program to display all the records in a file along with line/record number.
Question 34Write a method in Python to write multiple line of text contents into a text file "mylife.txt".
Question 35Write a method in Python to read the content from a text file "DIARY.TXT" line by line and display the same on the screen.
Question 36Write appropriate statements to do the following:(a) To open a file named "RESULT.DAT" for output.(b) To go to the end of the file at any...
Question 37Write a program to add two more employees' details (empno, ename, salary, designation) to the file "emp.txt" already stored in disk.
Question 38How are the following codes different from one another?1.fp = open("file.txt", 'r') fp.read()2.fp = open("file.txt", 'r')
Question 39What is the output of the following code fragment? Explain.fout = open("output.txt", 'w') fout.write("Hello, world! \n") fout.write("How...
Question 40Write the output of the following code with justification if the contents of the file "ABC.txt" are:"Welcome to Python Programming!"f1 =...
Question 41Give the output of the following snippet:import pickle list1, list2 = [2, 3, 4, 5, 6, 7, 8, 9, 10], [] for i in list1: if (i % 2 == 0...
Question 42Anant has been asked to display all the students who have scored less than 40 for Remedial Classes. Write a user-defined function to...
Question 43Following is the structure of each record in a data file named "PRODUCT.DAT".{"prod_code": value, "prod_desc": value, "stock": value} The...
Question 44Given a binary file "STUDENT.DAT", containing records of the following type:[S_Admno, S_Name, Percentage]Where these three values...
Question 45Write a statement to open a binary file C:\Myfiles\Text1.dat in read and write mode by specifying for file path in two different formats.
Question 46Write a statement in Python to perform the following operations:(a) To open a text file "BOOK.TXT" in read and append mode(b) To open a...
Question 47What is the following code doing?import csv File = open("contacts.csv", "a") Name = input("Please enter name: ") Phno = input("Please...
Question 48Write code to open the file in the previous question and print it in the following form:Name : <name> Phone: <phone number>
Question 49Consider the file "contacts.csv" created in the question above and figure out what the following code is trying to do?name = input("Enter...
Question 50Write a program to enter the following records in a binary file:Item No — integerItem_Name — stringQty — integerPrice — floatNumber of...
Question 51Create a CSV file "Groceries" to store information of different items existing in a shop. The information is to be stored w.r.t. each item...
Question 1An absolute path always begins with the root folder.
Question 2It is not necessary to always create the file in the same default folder where Python has been installed.
Question 3In binary file, there is no delimiter for a line.
Question 4A binary file stores information in ASCII or Unicode characters.
Question 5readline() reads the entire file at a time.
Question 6A close() function breaks the link of the file object and the file on the disk.
Question 7When we open a file in read mode, the given file must exist in the folder; otherwise, Python will raise FileNotFound error.
Question 8The default file open mode is write mode.
Question 9Opening a file in append mode erases the previous data.
Question 10A file mode defines the type of operations that is to be performed on the file.
Question 11A stream is defined as a sequence of characters.
Question 12readlines() function returns a list of strings, each separated by "\n".
Question 13Data maintained inside the file is termed as "persistent" (permanent in nature) data.
Question 14'with' statement ensures that all the resources allocated to the file objects get deallocated automatically.
Question 15csv module can handle CSV files correctly regardless of the operating system on which the files were created.
Question 16csv module gets automatically imported into the program for reading a CSV file.
Question 17The type of operation that can be performed on a file depends upon the file mode in which it is opened.
Question 18Functions readline() and readlines() are essentially the same.
Question 19Every record in a CSV file is stored in reader object in the form of a list.
Question 20writerow() method allows us to write a list of fields to the CSV file.
Question 21Comma is the default delimiter for a CSV file.
Question 22tell() method of Python tells us the current position within the file.
Question 23The offset argument to seek() method indicates the number of bytes to be moved.
Question 24If the offset value is set to 2, beginning of the file would be taken as seek position.