Beginner Codes/Python/Reading Files
78%

Instructions

Reading Files 📄

Namba program eppovume RAM-la dhaan run aagum. Program close aanadhum ellame azhinjidum. Permanant-a data-va save panra edam dhaan Files (Hard disk). Python vachu namba text files-a read panna mudiyum!

🔍 Deep Dive: Real-world Analogy (Reading a Book 📖)

Oru library-la irundhu book edukkareenga. First, andha book-a open pannanum. Apram ulla irukka kathaigalai read pannanum. Padichu mudichadhum marakkama book-a close pannanum, appo dhaan adutha aal padikka mudiyum. Files-um adhe madhiri dhaan!

1. Using "with" (The Safest Way)

with open() use panna, book-a padichu mudichadhum Python automatic-a adha close pannidum. Namba marandhalum problem illa!

# 'r' na Read mode. 'f' na namba file oda variable.
with open("data.txt", "r") as f:
    content = f.read()
    print(content)

Idha theliva purinjikka indha interactive visualizer-a test pannunga:

with open("data.txt", "r") as f:
content = f.read()
print(content)
# End of 'with' block
data.txt
📕
CLOSED
What is happening?
'Next Step' click pannunga, aarambikkalam!

💡 Did You Know? with illama verum f = open() use panni f.close() panna marandhutta, andha file "lock" aagidum. Vera entha program-um adha use panna mudiyadhu. So eppovume with use pandradhu dhaan best practice!

Your Turn! (Unga neram!)

Imagine pannikonga secret.txt nu oru file irukku, adhula "Eureka!" nu yezhudhi irukku. Andha file-a with open() use panni Read mode ("r")-la open pannunga. Apram f.read() use panni adhula irukuradha print panni paarunga!

Help

Reading FilesExercise 61/7735 XP
❴❵ script.py
python.code
python
Loading...
Terminal
Terminal — python
Beginner Codes Terminal v1.0.0
─────────────────────────────────────
Ready. Click "Run Code" to execute...
Ready
Piston APIPYTHON0 lines