Saturday, January 17, 2015

Overview

This post is about file io in python. More information can be found here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# reading a file in 1 shot
with open('filename.txt', 'r') as file:
    content = file.read()

# reading a file, getting all the lines
with open('filename.txt', 'r') as file:
    content = file.readlines()

# writing to a file/creating a file
with open('filename.txt', 'w') as file:
    file.write('This is output')

# appending to a file
with open('filename.txt', 'ag') as file:
    file.write('This is output')

Random Posts