1. Computer Science 151
An introduction to the art of computing
File Input and Ouptut
Rudy Martinez
2. CS151 Spring 2019
Opening Files in Python
● Open() function
○ Returns a file object
○ myFile = open(‘filename’, ‘mode’)
■ Mode:
● ‘r’ - Read only
● ‘w’ - write, allows you to write and edit files
○ This will overwrite an existing file! Be careful.
● ‘a’ - append will add to the end of the file.
myfile=open(‘Moby_Dick_by_Mellville.txt’, ‘r’)
3. CS151 Spring 2019
Using a file object
myfile=open(‘Moby_Dick_by_Mellville.txt’, ‘r’)
● To read a single line.
○ myfile.readline()
■ This returns a string
● To read all lines into a list.
○ textList = myfile.readlines()
■ This returns a list
4. CS151 Spring 2019
How to read all lines
● textList = myfile.readlines()
● How do we go through a list?
○ For loop!
# Open file
myfile=open('/home/rudym/Downloads/Moby_Dick_by_Melville.txt', 'r')
# Load each line in file to list textList
textList = myfile.readlines()
# For loop to read and print each line
for i in range(0, len(textList), 1):
print(textList[i])
5. CS151 Spring 2019
Writing Files
● Writing files is similar to reading files.
● The main misstep is using ‘w’ instead of ‘a’ when you have a file you want to
add to.
○ ‘w’ will write over any file
○ ‘a’ will append to the end of the file preserving previous entries
outfile = open(‘Filename’, ‘w’)
6. CS151 Spring 2019
Writing files
● Also like read, there are two main functions for putting text into a file.
○ write(‘string’) - will write a passed string into the file and does not append a carriage return
○ writelines( string, list, or dictionary ) - takes anything that can be iterated
■ Iterables are strings, list and dictionaries
outfile = open(‘test.txt’, ‘w’)
for i in range(0, 10, 1):
outfile.write(str(i))
Output: 0123456789
outfile = open(‘test.txt’, ‘w’)
myString = ‘Hello Cruel World’
outfile.writelines(myString)
Output: Hello Cruel World
7. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[H]
8. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[He]
9. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hel]
10. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hell]
11. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello]
12. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello ]
13. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello C]
14. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cr]
15. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cru]
16. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Crue]
17. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel]
18. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel ]
19. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel W]
20. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel Wo]
21. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel Wor]
22. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel Worl]
23. CS151 Spring 2019
Iterables
● An iterable is any data structure that has an iterator
● What is an iterator?
○ An iterator is a pointer that walks any data structure.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
myString H e l l o C r u e l W o r l d
[Hello Cruel World]
24. CS151 Spring 2019
For loops revisited!
Old For loop
for i in range(0, len(myString), 1):
print(myString[i])
Quick Loop
for i in myString:
print(i]
The two for loops below do the exact same thing, but one is short hand!
Why didn’t you teach us this first?!?*?
25. CS151 Spring 2019
To Sum it up
● Open returns a file object for reading and writing.
● Reading:
○ readline() - Single line of text
○ readlines() - All lines into list
● Writing:
○ writeline() - Single string to file
○ writelines() - An iterable to the file
26. CS151 Spring 2019
Friday
● 2/15 Homework 2 part A assigned
● 2/20 Homework 2 part A DUE! (5 days)
● 2/22 Homework 2 part B assigned
● 2/27 Homework 2 part B DUE! (5 days)
● 3/1 Homework 2 part C assigned
● 3/4 Exam Review!!!
● 3/6 Midterm Exam
● 3/29 Homework 2 part C DUE! (nearly a month)