Question on: SS2 ICT - Handling Computer File
How would you modify the code example provided to write data in separate lines?
View related lesson
Ask EduPadi AI for a detailed answer
You can modify the code by adding newline characters ("\n") at the end of each piece of data to ensure they are written on separate lines in the file. Here's how the modified code would look:
python Copy code
# Step 1: Open the file for writing
file = open("sequential_file.txt", "w")
# Step 2: Write data to the file
data1 = "Hello, "
data2 = "world!"
file.write(data1 + "\n")
file.write(data2 + "\n")
# Step 3: Close the file
file.close()
Add your answer
Please share this, thanks!
No responses