PYTHON Reference Guide
Revision Time: 2 mins

Files Reference

Opening file descriptors, reading line streams, and writing buffers.

open
Return: FileObject

Open file for reading or writing.

Used In: Log parsing, local file reading.

Syntax signature:open(file, mode='r', encoding=None)
Code snippet:
python
with open('tmp.txt', 'w') as f:
    f.write('data')
print('Saved')
Expected Output:'Saved'

Time Complexity: O(1)

Common Mistakes: Forgetting to specify the encoding (defaults to system locale, which can crash on UTF-8 characters on Windows).

Related Methods: read(), write()

Comparison:

read() vs readline(): read() loads the entire file into memory; readline() reads a single line at a time (memory-safe).

Remember: Always use context manager (with) to ensure descriptors are closed.