Authors: Annu Jolie
Edits: Pranav Shridhar
Suppose you are working on a very important project and you are entering some valuable data into your program. While a program is running, its data is stored in random access memory (RAM). RAM is fast and inexpensive, but it is also volatile, which means that when the program ends, or the computer shuts down, data in RAM disappears. Hence, if the power goes off all your hard work and valuable data goes into vain. To make data available the next time the computer is turned on and the program is started, it has to be written to a non-volatile storage medium, such a hard drive, usb drive, or CD-RW. Data on non-volatile storage media is stored as files.
Python files are the same as other files on your computer: documents, pictures, music, games . . . indeed, everything on your computer is stored as files.
Working with files is a lot like working with a notebook.
To use a notebook :
- It has to be opened.
- When done, it has to be closed.
- While the notebook is open, it can either be read from or written to.
- In either case, the notebook holder knows where they are. They can read the whole notebook in its natural order or they can skip around.
Similarly in python, a file operation takes place in the following order.
- Open a file
- Read or write (perform operation)
- Close the file
It's as simple as that. So lets get our hands dirty, and start writing out first file programClick here