File Handling: Open, Read, Write, Append and Close – Grade 13 A/L ICT Notes 9.11

© 2026 schoolict.net (Ruwan Mangala Suraweera)

9.11 File Handling: Open, Read, Write, Append and Closeගොනු හැසිරවීම: විවෘත, කියවීම, ලිවීම, එකතු කිරීම සහ වැසීම

Grade 13 A/L ICT · Lesson 913 ශ්‍රේණිය A/L ICT · පාඩම 9

Prints all tabs in the language you are reading. Use “Save as PDF” in the print window to keep a copy.ඔබ කියවන භාෂාවෙන් සියලු tabs මුද්‍රණය වේ. පිටපතක් තියාගන්න print window එකේ “Save as PDF” තෝරන්න.
🎬Video coming soon in this language. Switch language to watch the other video.මේ භාෂාවෙන් වීඩියෝව ඉක්මනින් එනවා. අනෙක් වීඩියෝව බලන්න භාෂාව මාරු කරන්න.

In this videoමේ වීඩියෝවේ

  • Files keep data after the program ends.ගොනු දත්ත ස්ථිරව තබයි.
  • open() with r, w or a.r, w, a සමඟ open().
  • “w” erases; “a” appends.“w” මකයි; “a” එකතු කරයි.
  • Read with read(), readline() or a for loop.read(), readline() හෝ for.
  • Always close() or use with.close() හෝ with.
Fileopen()close()Read modeWrite modeAppend moderead()

Notesසටහන්

Syllabus 9.11. Variables are lost when a program ends; files keep data on storage. In Python you open a file with a mode, read or write it, and close it. Mode “w” creates or empties a file, “a” appends to the end, and “r” reads.විෂය නිර්දේශය 9.11. වැඩසටහන අවසන් වූ විට විචල්‍ය නැති වේ; ගොනු දත්ත ගබඩාවේ තබා ගනී. Python හි mode එකක් සමඟ ගොනුව විවෘත කර, කියවා හෝ ලියා, වසන්න. “w” ගොනුව සාදයි හෝ හිස් කරයි, “a” අගට එකතු කරයි, “r” කියවයි.

1. File modes1. ගොනු modes

File modesopen(f, “w”)write – new/emptyopen(f, “a”)append – add to endopen(f, “r”)read linesclose()save and freeOpening in “w” mode erases an existing file – use “a” to keep old data.
The r, w and a modesr, w සහ a modes
ModeModeMeaningඅර්ථයIf file is missingගොනුව නැත්නම්
“r”“r”Read from the startමුල සිට කියවීමErrorදෝෂය
“w”“w”Write – old content is erasedලිවීම – පැරණි අන්තර්ගතය මැකේCreatedසාදයි
“a”“a”Append – add to the endඑකතු කිරීම – අගටCreatedසාදයි

🐍 Write, append and read a text fileපෙළ ගොනුවක් ලිවීම, එක් කිරීම සහ කියවීම

f = open("marks.txt", "w")      # write: creates or empties
f.write("Nimal,78\n")
f.write("Kamala,45\n")
f.close()

f = open("marks.txt", "a")      # append: adds to the end
f.write("Sunil,88\n")
f.close()

f = open("marks.txt", "r")      # read
total = 0
for line in f:
    name, mark = line.strip().split(",")
    total += int(mark)
    print(name, mark)
f.close()
print("Total:", total)

▶ Outputප්‍රතිදානය

Nimal 78
Kamala 45
Sunil 88
Total: 211
🧮 Worked exampleවිසඳූ උදාහරණය Why does the file have three lines and Total 211?ගොනුවේ පේළි තුනක් සහ Total 211 ඇයි?
“w” writes Nimal and Kamala; “a” keeps them and adds Sunil. Reading line by line, strip() removes the newline, split(“,”) separates name and mark, and int() converts it: 78 + 45 + 88 = 211.“w” Nimal සහ Kamala ලියයි; “a” Sunil එකතු කරයි. strip(), split(“,”), int(): 78 + 45 + 88 = 211.
  • read() returns the whole file; readline() one line; readlines() a list of lines.read() මුළු ගොනුව; readline() එක් පේළියක්; readlines() ලැයිස්තුවක්.
  • write() writes a string – add “\n” yourself for a new line.write() තන්තුවක් ලියයි – නව පේළියට “\n” එක් කරන්න.
  • close() saves the buffer and frees the file.close() buffer එක සුරකියි, ගොනුව නිදහස් කරයි.
💡 with open(“marks.txt”) as f: closes the file automatically, even if an error happens.with open(…) as f: දෝෂයක් වුවද ගොනුව ස්වයංක්‍රීයව වසයි.
🚀 Beyond the syllabus (2026 update)🚀 විෂය නිර්දේශයෙන් ඔබ්බට (2026 යාවත්කාලීනය) Not required for the exam – read for interest.විභාගයට අවශ්‍ය නැත – දැනුම සඳහා කියවන්න. In 2026 many apps store data as CSV or JSON files; Python’s csv and json modules read them in one line.2026 දී බොහෝ යෙදුම් CSV හෝ JSON ගොනු භාවිත කරයි; csv සහ json modules ඒවා පහසුවෙන් කියවයි.

Key Wordsප්‍රධාන පද

Fileගොනුව
Named collection of data kept on storage.ගබඩාවේ තබන නම් කළ දත්ත එකතුව.
open()open()
Built-in function that opens a file in a mode.mode එකකින් ගොනුවක් විවෘත කරන ශ්‍රිතය.
close()close()
Method that finishes using a file and saves it.ගොනුව භාවිතය අවසන් කර සුරකින method.
Read modeකියවීමේ mode
Mode r that reads an existing file.පවතින ගොනුවක් කියවන r mode.
Write modeලිවීමේ mode
Mode w that creates or empties a file.ගොනුවක් සාදන හෝ හිස් කරන w mode.
Append modeඑකතු කිරීමේ mode
Mode a that adds data to the end of a file.ගොනුවේ අගට දත්ත එක් කරන a mode.
read()read()
Method that returns the whole file as a string.මුළු ගොනුව තන්තුවක් ලෙස දෙන method.
readline()readline()
Method that returns one line of a file.ගොනුවේ එක් පේළියක් දෙන method.
write()write()
Method that writes a string to a file.ගොනුවට තන්තුවක් ලියන method.
Newlineනව පේළිය
Character \n that ends a line of text.පෙළ පේළියක් අවසන් කරන \n අක්ෂරය.
Text fileපෙළ ගොනුව
File of readable characters such as .txt or .csv..txt, .csv වැනි කියවිය හැකි අක්ෂර ගොනුව.
Persistent storageස්ථිර ගබඩාව
Storage that keeps data after the program ends.වැඩසටහන අවසන් වූ පසුද දත්ත තබා ගන්නා ගබඩාව.

Sample Q&Aආදර්ශ ප්‍රශ්න සහ පිළිතුරු

1Why use files?ගොනු ඇයි?

Answerපිළිතුර: Variables are lost when the program ends; files keep data.දත්ත ස්ථිරව තබා ගැනීමට.

2“w” vs “a”?“w” සහ “a”?

Answerපිළිතුර: “w” erases old content; “a” adds to the end.“w” මකයි; “a” එකතු කරයි.

3Opening a missing file with “r”?“r” නැති ගොනුව?

Answerපිළිතුර: An error (FileNotFoundError).දෝෂයක්.

4Why strip() each line?strip() ඇයි?

Answerපිළිතුර: To remove the newline before splitting.නව පේළිය ඉවත් කිරීමට.

5Total printed?Total?

Answerපිළිතුර: 211.211.

6Why close a file?වැසීම ඇයි?

Answerපිළිතුර: To save buffered data and free the file.දත්ත සුරැකීමට.

MCQබහුවරණ ප්‍රශ්න

Choose the correct answer for each question.සෑම ප්‍රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.

Scoreලකුණු: 0 / 20

1Which mode adds data to the end of a file?අගට

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Append.Append.

2Opening an existing file with “w”“w”

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Write empties.හිස් කරයි.

3The Total printed by the files program isTotal

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

78 + 45 + 88.78 + 45 + 88.

4How many lines does marks.txt hold at the end?පේළි

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Two written, one appended.2 + 1.

5line.strip().split(“,”) on “Nimal,78\n” givessplit

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Two strings.තන්තු දෙකක්.

6Which method returns one line?එක් පේළියක්

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

readline.readline.

7Opening a missing file in “r” mode“r”

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

FileNotFoundError.දෝෂය.

8The with statement for fileswith

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Auto close.ස්වයංක්‍රීයව වසයි.

9Which term means: “Named collection of data kept on storage”?“ගබඩාවේ තබන නම් කළ දත්ත එකතුව” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

File – Named collection of data kept on storage.ගොනුව – ගබඩාවේ තබන නම් කළ දත්ත එකතුව.

10Which term means: “Built-in function that opens a file in a mode”?“mode එකකින් ගොනුවක් විවෘත කරන ශ්‍රිතය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

open() – Built-in function that opens a file in a mode.open() – mode එකකින් ගොනුවක් විවෘත කරන ශ්‍රිතය.

11Which term means: “Method that finishes using a file and saves it”?“ගොනුව භාවිතය අවසන් කර සුරකින method” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

close() – Method that finishes using a file and saves it.close() – ගොනුව භාවිතය අවසන් කර සුරකින method.

12Which term means: “Mode r that reads an existing file”?“පවතින ගොනුවක් කියවන r mode” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Read mode – Mode r that reads an existing file.කියවීමේ mode – පවතින ගොනුවක් කියවන r mode.

13Which term means: “Mode w that creates or empties a file”?“ගොනුවක් සාදන හෝ හිස් කරන w mode” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Write mode – Mode w that creates or empties a file.ලිවීමේ mode – ගොනුවක් සාදන හෝ හිස් කරන w mode.

14Which term means: “Mode a that adds data to the end of a file”?“ගොනුවේ අගට දත්ත එක් කරන a mode” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Append mode – Mode a that adds data to the end of a file.එකතු කිරීමේ mode – ගොනුවේ අගට දත්ත එක් කරන a mode.

15Which term means: “Method that returns the whole file as a string”?“මුළු ගොනුව තන්තුවක් ලෙස දෙන method” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

read() – Method that returns the whole file as a string.read() – මුළු ගොනුව තන්තුවක් ලෙස දෙන method.

16Which term means: “Method that returns one line of a file”?“ගොනුවේ එක් පේළියක් දෙන method” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

readline() – Method that returns one line of a file.readline() – ගොනුවේ එක් පේළියක් දෙන method.

17Which term means: “Method that writes a string to a file”?“ගොනුවට තන්තුවක් ලියන method” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

write() – Method that writes a string to a file.write() – ගොනුවට තන්තුවක් ලියන method.

18Which term means: “Character \n that ends a line of text”?“පෙළ පේළියක් අවසන් කරන \n අක්ෂරය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Newline – Character \n that ends a line of text.නව පේළිය – පෙළ පේළියක් අවසන් කරන \n අක්ෂරය.

19Which term means: “File of readable characters such as .txt or .csv”?“.txt, .csv වැනි කියවිය හැකි අක්ෂර ගොනුව” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Text file – File of readable characters such as .txt or .csv.පෙළ ගොනුව – .txt, .csv වැනි කියවිය හැකි අක්ෂර ගොනුව.

20Which term means: “Storage that keeps data after the program ends”?“වැඩසටහන අවසන් වූ පසුද දත්ත තබා ගන්නා ගබඩාව” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Persistent storage – Storage that keeps data after the program ends.ස්ථිර ගබඩාව – වැඩසටහන අවසන් වූ පසුද දත්ත තබා ගන්නා ගබඩාව.

MCQ answer keyMCQ පිළිතුරු

  1. A – Append.Append.
  2. D – Write empties.හිස් කරයි.
  3. B – 78 + 45 + 88.78 + 45 + 88.
  4. C – Two written, one appended.2 + 1.
  5. B – Two strings.තන්තු දෙකක්.
  6. E – readline.readline.
  7. B – FileNotFoundError.දෝෂය.
  8. A – Auto close.ස්වයංක්‍රීයව වසයි.
  9. C – File – Named collection of data kept on storage.ගොනුව – ගබඩාවේ තබන නම් කළ දත්ත එකතුව.
  10. A – open() – Built-in function that opens a file in a mode.open() – mode එකකින් ගොනුවක් විවෘත කරන ශ්‍රිතය.
  11. E – close() – Method that finishes using a file and saves it.close() – ගොනුව භාවිතය අවසන් කර සුරකින method.
  12. D – Read mode – Mode r that reads an existing file.කියවීමේ mode – පවතින ගොනුවක් කියවන r mode.
  13. E – Write mode – Mode w that creates or empties a file.ලිවීමේ mode – ගොනුවක් සාදන හෝ හිස් කරන w mode.
  14. D – Append mode – Mode a that adds data to the end of a file.එකතු කිරීමේ mode – ගොනුවේ අගට දත්ත එක් කරන a mode.
  15. D – read() – Method that returns the whole file as a string.read() – මුළු ගොනුව තන්තුවක් ලෙස දෙන method.
  16. A – readline() – Method that returns one line of a file.readline() – ගොනුවේ එක් පේළියක් දෙන method.
  17. B – write() – Method that writes a string to a file.write() – ගොනුවට තන්තුවක් ලියන method.
  18. E – Newline – Character \n that ends a line of text.නව පේළිය – පෙළ පේළියක් අවසන් කරන \n අක්ෂරය.
  19. C – Text file – File of readable characters such as .txt or .csv.පෙළ ගොනුව – .txt, .csv වැනි කියවිය හැකි අක්ෂර ගොනුව.
  20. C – Persistent storage – Storage that keeps data after the program ends.ස්ථිර ගබඩාව – වැඩසටහන අවසන් වූ පසුද දත්ත තබා ගන්නා ගබඩාව.

Structured Questionsව්‍යුහගත ප්‍රශ්න

1Explain the r, w and a modes.modes තුන. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
r read; w create/erase; a append.r; w; a.

2Write code to save three names to a file, one per line.නම් තුනක් ලියන්න. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
open w; write each with \n; close.open; write; close.

3Write code to read marks.txt and print the total.total කියවන්න. (6 marksලකුණු 6)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
open r; loop lines; split; int; add; print.open; loop; int.

4Explain why the program prints Total 211.211 ඇයි? (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
w two lines; a adds Sunil; 78 + 45 + 88.w; a; එකතුව.

5State two reasons to close a file.වැසීම හේතු 2. (2 marksලකුණු 2)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
Flush data; free the file for others.සුරැකීම; නිදහස් කිරීම.

🎮 Game🎮 ක්‍රීඩා

Pick a game. Each one uses words from this lesson.ක්‍රීඩාවක් තෝරන්න. හැම එකක්ම මේ පාඩමේ වචන පාවිච්චි කරනවා.

Summary & Downloadsසාරාංශය සහ බාගත කිරීම්

Key pointsප්‍රධාන කරුණු

  • Files keep data after the program ends.ගොනු දත්ත ස්ථිරව තබයි.
  • open() with r, w or a.r, w, a සමඟ open().
  • “w” erases; “a” appends.“w” මකයි; “a” එකතු කරයි.
  • Read with read(), readline() or a for loop.read(), readline() හෝ for.
  • Always close() or use with.close() හෝ with.

Downloadsබාගත කිරීම්

Full Lesson 9 PDFසම්පූර්ණ පාඩම 9 PDF: English · සිංහල

Next noteඊළඟ සටහන 9.12 →

© 2026 schoolict.net (Ruwan Mangala Suraweera). All rights reserved.සියලු හිමිකම් ඇවිරිණි.

Resource Book Lesson Note Download Questions Syllabus Sinhala LMS Simulator Tool