Lesson 30 of 34

Files: लिखना और पढ़ना

9 min 30 XP

प्रोग्राम ख़त्म होने के बाद भी डेटा रखने के लिए उसे file में सहेजें। open(name, "w") लिखता है, "a" जोड़ता है और "r" पढ़ता है। with लगाने से file अपने-आप बंद हो जाती है।

Example: edit me and press Run

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

file को लाइन-दर-लाइन पढ़ने के लिए उस पर loop चलाएँ। strip() हर लाइन के आख़िर का \n हटा देता है।

Example: edit me and press Run

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

यहाँ files सिर्फ़ आपके ब्राउज़र में रहती हैं और refresh पर मिट जाती हैं। कंप्यूटर पर Python इंस्टॉल हो, तो असली files बनती हैं।

Challenge

+30 XP

names = ["arun", "priya", "kavi"]। हर नाम को "students.txt" में अलग लाइन में लिखें। फिर file वापस पढ़ें और हर नाम बड़े अक्षरों में (upper) print करें।

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