Lesson 30 of 34

Files: எழுதுதல் மற்றும் படித்தல்Files: Writing and Reading

9 min 30 XP

Program முடிந்த பிறகும் தகவல் இருக்க வேண்டுமென்றால் file-ல் சேமிக்க வேண்டும். open(பெயர், "w") எழுத, "a" கடைசியில் சேர்க்க, "r" படிக்க. with பயன்படுத்தினால் file தானாக மூடப்படும்.

To keep data after a program ends, save it to a file. open(name, "w") writes, "a" appends and "r" reads. Using with closes the file for you.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

for loop மூலம் file-ஐ வரி வரியாகப் படிக்கலாம். strip() ஒவ்வொரு வரியின் கடைசி \n-ஐ நீக்கும்.

Loop over a file to read it line by line. strip() removes the \n at the end of each line.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

இங்கே files உங்கள் browser-க்குள் மட்டுமே உருவாகும்; பக்கத்தை refresh செய்தால் அழிந்துவிடும். உங்கள் கணினியில் Python நிறுவினால் உண்மையான files உருவாகும்.

Here, files live only inside your browser and vanish on refresh. With Python installed on your computer, real files are created.

Challenge

+30 XP

names = ["arun", "priya", "kavi"]. ஒவ்வொரு பெயரையும் ஒரு வரியாக "students.txt" file-ல் எழுதுங்கள். பிறகு அந்த file-ஐப் படித்து, ஒவ்வொரு பெயரையும் பெரிய எழுத்துகளில் (upper) print செய்யுங்கள்.

names = ["arun", "priya", "kavi"]. Write each name on its own line in "students.txt". Then read the file back and print each name in capitals (upper).

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.