d

Python File IO

How to read and write files with Python

basic format

# f = open( <filename>, 'mode')
f = open( 'myfile.txt', 'w' )

modes

reference: https://docs.python.org/3/library/functions.html#open

Character Description
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)

Create

create

Write

write

Append

append

Read

read