while
and readlinefor
LoopI have posted homework 8 here.
It is due this coming Sunday at 11:59 PM.
I have posted the answers to Quiz 6 here.
Are there any questions before I begin?
$ 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 Boston Red Sox Baltimore Orioles Toronto Blue Jays Tampa Bay Rays New York Yankees
$ cat readline_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 readline_teams.py Boston Red Sox Baltimore Orioles Toronto Blue Jays Tampa Bay Rays
$ cat readline_teams_2.py # reads and prints the lines in teams.txt file = open('teams.txt', 'r') line_1 = file.readline() print(line_1.strip()) line_2 = file.readline() print(line_2.strip()) line_3 = file.readline() print(line_3.strip()) line_4 = file.readline() print(line_4.strip()) $ python3 readline_teams_2.py Boston Red Sox Baltimore Orioles Toronto Blue Jays Tampa Bay Rays
# 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
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
loop needs a line to test ...
$ 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.strip())
line = file.readline()
$ python3 while_loop_print_file.py
Filename: teams.txt
Boston Red Sox
Baltimore Orioles
Toronto Blue Jays
Tampa Bay Rays
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
loopin
?for
loop 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.strip())
$ python3 for_loop_print_file.py
Filename: teams.txt
Red Sox
Patriots
Celtics
Bruins
for
loop
for line in file: print(line.strip())
while
loop
line = file.readline() while line != ": print(line.strip()) line = file.readline()
for
loop when reading files