Python Fundamentals

Types of Files


Opening Files


Below are the options we can provide to the open() function for opening (and creating) a file:

<aside> 💡 When you open in write w mode, the file is truncated, i.e. existing contents are deleted.

</aside>

#f will be a file object
f = open('filename',mode='wt',encoding='utf-8')

At the filesystem level, files are stored as a system of bytes. Files opened in binary mode, read and write their contents as bytes object. Binary mode reflects the raw data in the file.

<aside> 💡 When providing complete path for filenames, use raw strings, for example, open(r”/Users/johndoe/Downloads/filename.txt”) which ensures that backslash (or forward slash in Windows) is treated as a literal character.

</aside>