while
and readlinefor
LoopI have posted homework 8 here.
It is due this coming Sunday at 11:59 PM.
$ cat fruit.txt grapes pears oranges cranberries apples melons blueberries
od
(octal dump) command
$ od -b fruit.txt 0000000 147 162 141 160 145 163 012 160 145 141 162 163 012 157 162 141 0000020 156 147 145 163 012 143 162 141 156 142 145 162 162 151 145 163 0000040 012 141 160 160 154 145 163 012 155 145 154 157 156 163 012 142 0000060 154 165 145 142 145 162 162 151 145 163 012 0000073
od
with the -c
option
od -c fruit.txt 0000000 g r a p e s \n p e a r s \n o r a 0000020 n g e s \n c r a n b e r r i e s 0000040 \n a p p l e s \n m e l o n s \n b 0000060 l u e b e r r i e s \n 0000073
open
open
function does two things
open
function
FILE_VARIABLE = open(FILENAME, MODE)
Mode | Description |
---|---|
r | Open a file for reading only |
w | Open a file for writing |
a | Open a file for writing to the end of the file |
OBJECT_VARIABLE.METHOD_NAME
teams_file = open("teams.txt", "w")
$ cat write_teams.py # writes the names of the teams in the American League East teams_file = open("teams.txt", "w") teams_file.write("Boston Red Sox\n") teams_file.write("Baltimore Orioles\n") teams_file.write("Toronto Blue Jays\n") teams_file.write("Tampa Bay Rays\n") teams_file.write("New York Yankees\n") $ python3 write_teams.py $ cat teams.txt Boston Red Sox Baltimore Orioles Toronto Blue Jays Tampa Bay Rays New York Yankees
$ cat read_teams.py # reads the contents of the file teams.txt teams_file = open("teams.txt", "r") teams = teams_file.read() print(teams) $ python3 read_teams.py Boston Red Sox Baltimore Orioles Toronto Blue Jays Tampa Bay Rays New York Yankees
$ cat teams.txt Red Sox Patriots Celtics Bruins
$ cat read_teams.py # reads and prints the lines in teams.txt file = open('teams.txt', 'r') line_1 = file.readline() print(line_1) line_2 = file.readline() print(line_2) line_3 = file.readline() print(line_3) line_4 = file.readline() print(line_4)
$ python3 read_teams.py Red Sox Patriots Celtics Bruins
print
adds a newline to the end of what it printsprint
makes it go down another line$ cat read_teams_2.py # reads and prints the lines in teams.txt file = open('teams.txt', 'r') line_1 = file.readline() print(line_1.rstrip()) line_2 = file.readline() print(line_2.rstrip()) line_3 = file.readline() print(line_3.rstrip()) line_4 = file.readline() print(line_4.rstrip())
$ python3 read_teams_2.py Red Sox Patriots Celtics Bruins
# creates a series of random numbers and writes them to a file # this script will not work import random filename = input("Filename: ") how_many = int(input("How many numbers: ")) highest = int(input("Highest number: ")) file = open(filename, "w") for i in range(how_many): file.write(random.randint(1, highest))
$ python3 random_numbers_create.py
Filename: numbers.txt
How many numbers: 10
Highest number: 5
Traceback (most recent call last):
File "random_numbers_create.py", line 11, in <module>
file.write(random.randint(1, highest))
TypeError: write() argument must be str, not int
# creates a series of random numbers and writes them to a file
import random
filename = input("Filename: ")
how_many = int(input("How many numbers: "))
highest = int(input("Highest number: "))
file = open(filename, "w")
for i in range(how_many):
file.write(str(random.randint(1, highest)) + "\n")
$ python3 random_numbers_create_2.py Filename: numbers.txt How many numbers: 10 Highest number: 5
$ cat numbers.txt 2 2 4 2 5 4 3 3 1 5
int
or float
to convert them back into numbers$ cat sox_games.txt 2017-04-04 Red Sox @ Astros Win 7-5 2017-04-05 Red Sox @ Astros Win 2-1 2017-04-07 Red Sox vs Yankees Loss 7-9
# adds the results of a Red Sox game to the file
date = input("Date: ")
opponent = input("Opponent: ")
sox_score = input("Sox score: ")
opp_score = input("Opponent score: ")
home_game = input("Was this a home game (y/n): ")
result = "Win"
if int(sox_score) < int(opp_score):
result = "Loss"
line = date + "\tRed Sox "
if home_game == "y":
line += "vs"
else:
line += "@"
line += " " + opponent + "\t" + result + " " + sox_score + "-" + opp_score + "\n"
game_file = open("sox_games.txt", "a")
game_file.write(line)
$ python3 sox_game_add.py Date: 2017-04-08 Opponent: Yankees Sox score: 12 Opponent score: 1 Was this a home game (y/n): y $ cat sox_games.txt 2017-04-04 Red Sox @ Astros Win 7-5 2017-04-05 Red Sox @ Astros Win 2-1 2017-04-07 Red Sox vs Yankees Loss 7-9 2017-04-08 Red Sox vs Yankees Win 12-1
while
loops
while
loops first?while
and readline
open the file for reading
call readline to get the first line
while the line is not the empty string:
process the last line from the file
read in the next line
while
loopwhile
loop
$ cat while_loop_print_file.py
# reads and prints the lines in a file
# using readline and a while loop
filename = input("Filename: ")
file = open(filename, "r")
line = file.readline()
while line != "":
print(line.rstrip())
line = file.readline()
$ python3 while_loop_print_file.py
Filename: teams.txt
Red Sox
Patriots
Celtics
Bruins
for
Loopwhile
loop
for
loopfor
loop
for LOOP_VARIABLE in LIST_OF_SOME_KIND:
STATEMENT
STATEMENT
...
for
loop can use anything which is a collection of thingsfor
loop?for
that reads a file and prints each line
$ cat for_loop_print_file.py
# reads and prints the lines in a file
filename = input("Filename: ")
file = open(filename, "r")
for line in file:
print(line.rstrip())
$ python3 for_loop_print_file.py
Filename: teams.txt
Red Sox
Patriots
Celtics
Bruins
for
loop
for line in file: print(line.rstrip())
while
loop
line = file.readline() while line != "": print(line.rstrip()) line = file.readline()
for
loop when reading files