LESSON: FILES
FUNCTION VIEW OF A COMPUTER
- Upon program execution, all data (e.g., variables) related to the program are loaded from the
secondary memory to the main memory.
WHAT IS A FILE?
- Named locations on the computer’s disk that permanently store information for future use of its
data.
- Used to permanently store data in non-volatile memory (e.g., hard disk) as opposed to volatile
sources like RAM
TEXT FILES AND BINARY FILES
1. Test (ASCII) File
- Consists of ASCII characters
- Usually created by using a text editor such as Notepad
- ASCII table
o Original: 7-bit character set
o Extended: 8-bit character set
2. Binary File
- Executable files, image files, sound files, etc.
- File that contains bits of 1s and 0s
- Strictly speaking, all files are binary files.
ESCAPE SEQUENCE
- A sequence of characters that has a “special” meaning.
Escape Sequence Meaning
\\ Backslash
\’ Single quote
\” Double quote
\n New line
\t Tab
\b Backspace
FILE HANDLING: open() Function
- Syntax: fileHandle = open(filename, mode)
o Filename – name of the file you want to open
o Mode – mode in which the file will be opened
o fileHandle – object for Python to access the file
- Modes
o ‘r’ – for reading (default)