© 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
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.
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
| ModeMode | Meaningඅර්ථය | 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
“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 එක සුරකියි, ගොනුව නිදහස් කරයි.
Key Wordsප්රධාන පද
Sample Q&Aආදර්ශ ප්රශ්න සහ පිළිතුරු
1Why use files?ගොනු ඇයි?
2“w” vs “a”?“w” සහ “a”?
3Opening a missing file with “r”?“r” නැති ගොනුව?
4Why strip() each line?strip() ඇයි?
5Total printed?Total?
6Why close a file?වැසීම ඇයි?
MCQබහුවරණ ප්රශ්න
Choose the correct answer for each question.සෑම ප්රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.
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 පිළිතුරු
- A – Append.Append.
- D – Write empties.හිස් කරයි.
- B – 78 + 45 + 88.78 + 45 + 88.
- C – Two written, one appended.2 + 1.
- B – Two strings.තන්තු දෙකක්.
- E – readline.readline.
- B – FileNotFoundError.දෝෂය.
- A – Auto close.ස්වයංක්රීයව වසයි.
- C – File – Named collection of data kept on storage.ගොනුව – ගබඩාවේ තබන නම් කළ දත්ත එකතුව.
- A – open() – Built-in function that opens a file in a mode.open() – mode එකකින් ගොනුවක් විවෘත කරන ශ්රිතය.
- E – close() – Method that finishes using a file and saves it.close() – ගොනුව භාවිතය අවසන් කර සුරකින method.
- D – Read mode – Mode r that reads an existing file.කියවීමේ mode – පවතින ගොනුවක් කියවන r mode.
- E – Write mode – Mode w that creates or empties a file.ලිවීමේ mode – ගොනුවක් සාදන හෝ හිස් කරන w mode.
- D – Append mode – Mode a that adds data to the end of a file.එකතු කිරීමේ mode – ගොනුවේ අගට දත්ත එක් කරන a mode.
- D – read() – Method that returns the whole file as a string.read() – මුළු ගොනුව තන්තුවක් ලෙස දෙන method.
- A – readline() – Method that returns one line of a file.readline() – ගොනුවේ එක් පේළියක් දෙන method.
- B – write() – Method that writes a string to a file.write() – ගොනුවට තන්තුවක් ලියන method.
- E – Newline – Character \n that ends a line of text.නව පේළිය – පෙළ පේළියක් අවසන් කරන \n අක්ෂරය.
- C – Text file – File of readable characters such as .txt or .csv.පෙළ ගොනුව – .txt, .csv වැනි කියවිය හැකි අක්ෂර ගොනුව.
- 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ආදර්ශ පිළිතුර පෙන්වන්න
2Write code to save three names to a file, one per line.නම් තුනක් ලියන්න. (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
3Write code to read marks.txt and print the total.total කියවන්න. (6 marksලකුණු 6)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
4Explain why the program prints Total 211.211 ඇයි? (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
5State two reasons to close a file.වැසීම හේතු 2. (2 marksලකුණු 2)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
🎮 Game🎮 ක්රීඩා
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බාගත කිරීම්
Resource Book Lesson Note Download Questions Syllabus Sinhala LMS Simulator Tool
