Skip to content

Latest commit

 

History

History
28 lines (18 loc) · 632 Bytes

with-files.md

File metadata and controls

28 lines (18 loc) · 632 Bytes

Read files using with

"""
Use with - as for reading and writing
With closes file automatically after the code in the statement has finished
"""

# read all lines into list
with open("test.txt") as fhand:
    all_lines = fhand.readlines()

# write line into file
with open("test.txt", "w") as fhand:
    fhand.write("This is a test.")

Reference and read more

The with statement

Reading and Writing Files