Python Programming - Reading and Writing Files

Exercise : Reading and Writing Files - General Questions
  • Reading and Writing Files - General Questions
81.
What is the purpose of the file.flush() method?
Flushes the internal buffer to the file
Reads the file content
Closes the file
Moves the file cursor to the beginning
Answer: Option
Explanation:
file.flush() flushes the internal buffer to the file.

82.
How can you append the text "Additional content" to an existing file named "existing.txt"?
with open("existing.txt", "a") as file:
    file.write("Additional content")
append_text("existing.txt", "Additional content")
with open("existing.txt", "w") as file:
    file.append("Additional content")
with open("existing.txt", "a") as file:
    file.append("Additional content")
Answer: Option
Explanation:
Using open() in append mode to add content to an existing file.

83.
What does the file.readline() method do?
Reads the entire file content
Reads a specific line from the file
Reads the last line of the file
Reads the next line from the file
Answer: Option
Explanation:
file.readline() reads the next line from the file.

84.
How can you check if a file named "data.txt" exists in the current directory?
if check_exists("data.txt"):
if file.exists("data.txt"):
if os.path.isfile("data.txt"):
if is_file("data.txt"):
Answer: Option
Explanation:
Using os.path.isfile() to check if the path refers to a regular file.

85.
What is the purpose of the file.truncate() method?
Closes the file
Removes specified characters from the file
Changes the file's access mode
Truncates the file at the current position
Answer: Option
Explanation:
file.truncate() truncates the file at the current position.