Skip to content

Oct 27: Reading and Writing Files

Learning Objectives

After today's class, you should be able to:

  • Create a new text file, and output several lines to the file.
  • Open an existing file, and append several lines to the file.
  • Read a text file line by line, and extract data from the file.

Reminders

POGIL Activity

  • FileIO.pdf
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours

Model 1

write.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
outfile = open("out.txt", "w")
outfile.write("Example ")
outfile.write("output ")
outfile.write("text file\n")
outfile.write("xyz Coordinates\n")
outfile.write("MODEL\n")
outfile.write(f"ATOM {1:3d}")
seq = f"n {0:5.1f}{1:5.1f}{2:5.1f}"
outfile.write(seq)
outfile.write("\n")
outfile.close()

Model 2

afile.write("new line\n")
afile = open("out.txt", "a")
afile.write("new line\n")
afile.write(2.0)
afile.write("2.0")
afile.close()
afile.write("new line\n")

After running the above code, the file contents should be:

out.txt
1
2
3
4
5
6
Example output text file
xyz Coordinates
MODEL
ATOM   1n   0.0  1.0  2.0
new line
2.0

Model 3

infile = open("out.txt", "r")
infile.readline()
infile.readline()
infile.readlines()
infile.readline()
infile.close()
infile = open("out.txt", "r")
for line in infile:
    print(line)
infile.close()
infile = open("out.txt", "r")
for i in range(3):
    infile.readline()
line = infile.readline()
infile.close()
line
line[0]
line[0:5]
words = line.split()
words
words[0]

The following file can be used for the last question:

names.txt
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
Macey Rowe
Cristal Munoz
Kyla Daniel
Marlene Mueller
Elliott Hart
Jimmy Cervantes
Chaya Schneider
Mariela Chambers
Kaeden Liu
Christine Potts
Juliet Cox
Marlee Johnston
Deshawn Braun
Irvin Larsen
Kathleen Blankenship
Deven Jefferson
Heidi Stein
Rhianna Anderson
Charlize Watts
Jocelyn Underwood
Deborah Faulkner
Jaiden Bowers
Rebecca Pratt
Jordan Chase
Roselyn Tanner
Ryland Kent
Makai Ray
Allison Peck
Savannah Wright
Lucia Koch
Ali Walsh
Kayleigh Hess
Chaz Pruitt
Lillie James
Amaris Oneill
Marie Campbell
Annabella York
Makayla Li
Linda Mcintosh
Valerie Eaton
Kenya Rich
Fatima Hartman
Kaya Scott
Jordan Gates
Carleigh Crane
Chasity Rios
Isaias Porter
Maleah Lawrence
Tiana Carroll
Perla Castaneda
Kyra Salas
Casey Gross
Regina Heath
Jackson Sawyer
Arielle Perkins
Ezekiel Zuniga
Jaslyn Stout
Osvaldo Henry
Maribel Richardson
Maryjane Webb
Isaiah French
Priscilla Caldwell
Giada Bean
Braeden Graves
Harmony Cole
Jerry Golden
Allisson Davila
Rosa Moss
Taylor Hunter
Armani Mcneil
Reyna Dickerson
Damaris Arnold
Finn Mcmillan
Cherish Le
Katherine Velez
Emmalee Blanchard
Griffin Hardy
Alejandra Barrett
Angel Welch
Dale Parrish
Mikaela Hebert
Willow David
Makenna Dudley
Jefferson Downs
Conrad Griffith
Amiya Ramsey
Esther Perez
Amina Singleton
Kaleigh Livingston
Janiyah Harvey
Troy Burns
Aidan Grimes
Odin Savage
Antonio Mitchell
Koen Morrison
Lindsay Novak
Ronald Flores
Paityn Hooper
Alec Pearson
Malik Carr