Python - File Reading and Writing Strategies
Which of the following Python code snippets correctly opens a file named
'data.txt' and reads its entire content into a variable text?'data.txt' and reads its entire content into a variable text?open with mode 'r' (read) and a with statement for safe handling.f.read() which reads the whole file content into text.readline(). with open('data.txt') as f:
text = f.readlines() reads lines into a list, not a string. file = open('data.txt', 'w')
text = file.read()
file.close() opens file in write mode, so reading is invalid.with open(..., 'r') and read() for full content [OK]15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions