Concept Flow - File modes and access types
Open file with mode
Check mode type
Read data
Close file after operation
Open a file with a mode, then read, write, or append data accordingly, and finally close the file.
f = open('test.txt', 'w') f.write('Hello') f.close()
| Step | Action | File Mode | Operation | Result |
|---|---|---|---|---|
| 1 | Open file | 'w' | Open for writing | File created or emptied |
| 2 | Write data | 'w' | Write 'Hello' | Data written to file |
| 3 | Close file | 'w' | Close file | File saved and closed |
| 4 | End | - | - | File closed, operations complete |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 |
|---|---|---|---|---|
| f | None | File object opened in 'w' mode | File object with 'Hello' written | File object closed |
File modes control how you open files: 'r' = read only (file must exist) 'w' = write only (creates or empties file) 'a' = append (add data to end) Always close files to save changes and free resources.