0% found this document useful (0 votes)
64 views6 pages

Python File Handling Practice Questions

Uploaded by

Khushee Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views6 pages

Python File Handling Practice Questions

Uploaded by

Khushee Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Python File Handling Practice Questions – Test 1
  • Python File Handling Practice Questions – Test 2
  • Python File Handling Practice Questions – Test 3
  • Python File Handling Practice Questions – Test 4

Python File Handling Practice Questions –

Test 1
Q1. Name two types of data files available in python .
Hide Answer
Ans. Text File and Binary File
Q2. In text file each line is terminated by a special character called _______
Hide Answer
Ans. EOL (End of Line)
Q3. In python, default EOL character is _______
Hide Answer
Ans. \n
Q4. Is there any delimiter of line in binary File?
Hide Answer
Ans. No
Q5. In binary files, data stored in the same format as it is stored in computer
memory. (T/F)
Hide Answer
Ans. True
Q6. Processing of binary file is faster than text file (T/F)
Hide Answer
Ans. True
Q7. Writing data is same as appending data in file. (T/F)
Hide Answer
Ans. False
Q8. Can we read file without opening. (T/F)
Hide Answer
Ans. False
Q9. Which function is used to open a file?
Hide Answer
Ans. open()
Q10. Write the code in python to open a file named “[Link]”
Hide Answer
Ans. f = open(“[Link]”)
Q11. Write one basic difference between Text file and Binary file in Python.
Hide Answer
Ans. A text file consists of human readable characters, which can be opened
by any text editor. while, binary files consist of non-human readable
characters and symbols, which require specific programs to access its
contents.

Python File Handling Practice Questions –


Test 2
Q1. f = open(“[Link]”)
Consider the code given above and write the answers of the following:
1. Identify name of the file.
2. Identify name of the function used above.
3. What is ‘f’ in above code?
4. The above statement will __________ file in ________ mode.
Hide Answer
Ans.
1. [Link]
2. open()
3. file object
4. open, read
Q2. What is the default mode of opening a file?
Hide Answer
Ans. read mode

Q3. Write a statement in python to open a file named “[Link]” stored in


folder “content” in D drive.
Hide Answer
Ans. f = open(“d:\\content\\[Link]”)
OR
f = open(r, “d:\content\[Link]”)
Q4. What error is returned by following statement, if file “[Link]” does not
exist?
f = open(“[Link]”)
Hide Answer
Ans. FileNotFoundError
Q5. Name the three modes in which file can be opened in python.
Hide Answer
Ans. Read, Write and Append
Q6. What is the purpose of ‘r’ as prefix in the given statement?
f = open(r, “d:\color\[Link]”)
Hide Answer
Ans. ‘r’ makes the string as raw string, means there is no special character in
string.
Q7. What do you mean by file object in python?
Hide Answer
Ans. File object is
reference to a file. It is used to read and write data to a file.
Q8. Another name of file object is _______
Hide Answer
Ans. File Handle
Q9. Write the symbols used in text file mode for the following operations.
1. Read Only
2. Write only
3. Read and Write
4. Write and Read
Hide Answer
Ans.
1. r
2. w
3. r+
4. w+
Q10. Write the symbols used in binary file mode for the following
operations.
1. Read Only
2. Write only
3. Read and Write
4. Write and Read
Hide Answer
Ans.
1. rb
2. wb
3. rb+
4. wb+
Python File Handling Practice Questions –
Test 3
Q1. What is the difference between r+ and w+ mode?
Hide Answer
Ans. r+ mode will return error if file does not exist while w+ mode will
create a new file if file does not exist.
Q2. What is the difference between write and append mode?
Hide Answer
Ans. Write mode over writes 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?
Hide Answer
Ans. close()
Q4. Write the statement to close the file “[Link]” which is associated with
file object named “fo”.
Hide Answer
Ans. [Link]()
Q5. Write one similarity and one difference between a+ and w+.
Hide Answer
Ans. Similarity : In both the modes, we can do read and write operations.
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?
Hide Answer
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() ?
Hide Answer
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 named “[Link]”
Ans.
f = open(“[Link]”,”r”)
data = [Link](10)
print(data)
Q9. Write a program to read entire content from the file named “[Link]”
Hide Answer
Ans.
f = open(“[Link], “r”)
d = [Link]()
print(d)
Q10. Write the output of the following, if the data stored in file “[Link]” is
given below. (“f” is the file object associated with file)
Try Try but never cry
print([Link](4))
print([Link](5))
print([Link]())
Hide Answer
Ans.
Try

Try b

ut never cry

Python File Handling Practice Questions –


Test 4
Q1. Write a program in python to read entire content of file (“[Link]”)
Hide Answer
Ans.
f = open("[Link]","r")
l = [Link]()
print(l)

Q7. Name two functions which are used to write data into file.
Hide Answer
Ans. write() and writelines()
Q8. Accept five names from the user and write in a file “[Link]”
Hide Answer
Ans.
f = open("[Link]","w")
for i in range(5):
n = input("Enter name")
[Link](n)
[Link]()
Q9. Accept five sports names from the user and write in a file “[Link]”
(each name should write in separate line)
Hide Answer
Ans
f = open("[Link]","w")
for i in range(5):
n = input("Enter sport name")
[Link](n)
[Link]('\n')
[Link]()
Q10. Accept five hobbies from the user and write in a file “[Link]” (each
hobby should write in separate line) without using write() function.
Hide Answer
Ans.
f = open("[Link]","w")
h=[]
for i in range(5):
n = input("Enter hobby name")
[Link](n,'\n')
[Link](h)
[Link]()
Disclaimer : I tried to give the correct code of all the above important
questions of Python File handling, but there may be some typing error in
above Python File Handling article or if any code given above is not clear to
you , share your valuable feedback on csiplearninghub@[Link]

Common questions

Powered by AI

'readline()' reads a single line from a file each time it is called, whereas 'readlines()' reads all lines in a file and returns them as a list. 'readline()' is useful for processing files line-by-line, while 'readlines()' is ideal for handling smaller files as a whole .

Binary files are processed faster than text files because the data is stored in the same format as it is stored in computer memory, which avoids any character conversion that text files might require. In contrast, text files consist of human-readable characters, necessitating conversion when being processed .

The 'rb+' mode is preferred when you want to read and write binary data to a file without deleting its existing content, as the mode allows reading and writing, without truncating the file. In contrast, 'wb+' mode creates a new file or overwrites an existing one, which may result in data loss if not intended .

The 'read()' function reads the entire content of a file, which is useful when the full data is needed at once (e.g., reading a configuration file). Meanwhile, 'read(n)' reads up to 'n' characters and is beneficial for streaming large files or systems with limited memory resources, enabling chunk-based processing .

The 'a+' mode is more beneficial than 'w+' when one needs to append data to a file without losing the existing data. The 'w+' mode truncates the file, deleting its previous contents, while 'a+' retains the old content and allows writing new data at the end .

'write()' is used for writing a single string to a file, while 'writelines()' is used to write an iterable of strings, such as a list, to a file. 'writelines()' does not add newline characters to the written strings, so they must be explicitly specified if needed .

A file object in Python acts as a reference to a file, allowing a program to perform read and write operations on the file. It serves as a bridge between the file stored on disk and the buffer used by the program .

The 'w+' file mode differs from 'r+' in that 'w+' will create a new file if the specified file does not exist, whereas 'r+' will return an error if the file does not exist .

Resource deallocation in Python file handling is ensured by using the 'close()' method on file objects after operations are completed, freeing up system resources. Furthermore, the 'with' statement (context manager) is recommended as it automatically handles file closing even if exceptions occur during file operations .

Using 'r' to define a string as a raw string is beneficial in file handling because it suppresses the special treatment of backslashes. This means path strings are used as given without inadvertently treating backslashes as escape characters, which is crucial for paths on Windows platforms .

Python File Handling Practice Questions – 
Test 1
Q1. Name two types of data files available in python .
Hide Answer
Ans. Tex
Ans. A text file consists of human readable characters, which can be opened 
by any text editor. while, binary files consist
Ans. Read, Write and Append
Q6. What is the purpose of ‘r’ as prefix in the given statement?        
f = open(r, “d:colorfl
Python File Handling Practice Questions – 
Test 3
Q1. What is the difference between r+ and w+ mode?
Hide Answer
Ans. r+ mode
print(data)
Q9. Write a program to read entire content from the file named “test.txt”
Hide Answer
Ans. 
f = open(“test.txt, “
   f.write(n)
f.close()
Q9. Accept five sports names from the user and write in a file “sport.txt” 
(each name should write i

You might also like