1/26
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
file.read() =
What reads an entire file into ONE BIG STRING?
file.readline()
What reads and returns the next line, returns empty string at end of file.
file.readlines()
What reads the entire file into a list of strings
What do each of readlines, readline, and read leave at the end of the long string?
\n automatically
.split()
Breaks into a list of single characters, can take an argument of separator or maxsplit (max number of splits done on a string)
Write a function to read while using a while look
file = open
line = file.readline()
while line in lines.....
function body
file.close()
Write a function to print all last names on a names document
infile = open("names.txt", "r")
lines = infile.readlines()
for line in lines
words = line.split()
print(words[1])
infile.close()
f refers to the file and has just been opened. WHat is printed?
f = open("songs.txt", "r"
line = f.readline()
line = f.readline()
while line[0] == "#":
line = f.readline()
print(f.readline())
Songs Chosen this quarter
# some names for the songs
# all good songs
Sep 1
Sep 14
The 4th line is printed cause it reads the first two lines, then the while look until the condition is no longer met, then exits the loop and continues on the bottom
f refers to the file and has just been opened. What is the purpose of repeating f.readline several times?
f = open("songs.txt", "r"
line = f.readline()
line = f.readline()
while line[0] == "#":
line = f.readline()
print(f.readline())
Songs Chosen this quarter
# some nmaes for the songs
# all good songs
Sep 1
Sep 14
Change this so the last line printed is the 4th line?
f = open("songs.txt", "r"
line = f.readline()
line = f.readline()
while line[0] == "#":
line = f.readline()
print(f.readline())
Songs Chosen this quarter
# some names for the songs
# all good songs
Sep 1
Sep 14
Instead of print(f.readline()), just print(line)
This does not give another readline command, just calls on an old one
Each line is printed in double lined format. Why?
infile = open("out.txt", "r")
for line in infile
print(line)
Double spaced, because the file itself already contains \n by default
If there is a number, what to do before using the ________ function?
Convert to string before using write function for files
PURPOSE of text files in python?
Permanent / persistent data storage
List TWO advantages of text files compared to keyboard and written data:
1. Data does not have to be entered on every program run
2. Programs have a way to write permanent output
Write a function to open a file in python: the most basic
def open_file_function(filepath):
with open(name, "r") as newfilename:
return newfilename.read()
List 3 MODES of opening a file
"r" = read only. Cannot create a new file - raises error
"w" = write only. Overwrites the file if it exists, or creates a new one.
"a" = append only. Adds data to end. Creates file if it doesn't exist.
Write an alternative function to open a file
with open(file_path, mode = "r") as filename:
newvariable = file.read()
Write a FOR LOOP to read a file
file_path = open("titleoffile.txt")
for line in file_path:
print(line.strip())
Write a WHILE LOOP to read a file
another_file_path = open("titleofanotherfile.txt")
line = another_file_path.readline() # readline reads a single file or line and then prints it
while line:
print(line.strip())
line = another_file_path.readline()
If not specified, what is the default mode for OPEN function in files?
Read mode, will not open if file does not already exist
List two things that makes the readline() function stop reading and printing?
1. \n new line
2. End of file
What does close() do?
1. Stops reading / writing the file
2. Prevents losing data
3. Releases the file so other programs and functions can reference it
Write a FUNCTION splitting the words in a file and printing out only the first name
infile = open("first and last names.txt", "r") # read mode is sufficient, since we are printing to the screen not the file
for line in infile:
words = line.split()
print(words[1])
outside loop: infile.close
IDENTIFY THE DIFFERENCE
infile = open("first and last names.txt", "r")
lines = infile.readlines()
for line in infile:
words = line.split()
print(words[1])
outside loop: infile.close
infile = open("first and last names.txt", "r")
for line in infile:
words = line.split()
print(words[1])
outside loop: infile.close
Both are functional, second one reads lines one at a time. More memory efficient.
Write a program that asks the user for the name of a file.
Program should rewrite file contents so that each line is preceded with a line number followed by a colon
Line numbering starts at 1
filename_input = input("Enter name of the file")
filename_input = str(filename_input) # ENSURES CORRECT FORMAT
infile = open("filename_input.txt", "r")
lines = filename_input.readlines()
infile.close()
outfile = open("filename_input.txt", "w")
for i in range(1, len(lines) + 1):
outfile.write(str(i) + lines[i])
outfile.close() # NECESSARY TO SEE RESULTS
A program is designed to retrieve data from a file, process it, and output the revised data to another file. Which of the following functions / methods will NOT be called in the program?
Open
A loop or method for reading (read, readlines, or readline)
write
close
All should be called
All should be called. Write should not be called if it is only outputting into the terminal.
To write another file, write is necessary
BIGGEST distinction between readline and read vs READLINES
Readlines returns a list of strings. THe other two only return ONE SINGLE STRING