Home › Python with Chai › Lesson 12

Files: read the bill back

Python with Chai, lesson 12 of 20. A short Hinglish video, simple English notes and code you can run, free.

Open the interactive lesson with Run button

The idea

open("bill.txt", "r") opens a file for reading, and read() gives you all its text.

"w" wipes the file and writes it fresh; "a" (append) keeps what is there and adds to the end.

So yesterday's bill is safe: read it back, and add today's orders below it.

The code

with open("bill.txt", "r") as f:
    bill = f.read()
print(bill)

with open("bill.txt", "a") as f:
    f.write("\nAdrak: Rs 15")

Output:

Total: Rs 50

Try it yourself

Remember

r = read, w = write fresh, a = add to the end.

← Files: save the bill · Modules: the lucky chai →

All lessons: Python with Chai