CS 150 Quiz 0 Review (In class and prep) - Files

0.0(0)
studied byStudied by 0 people
0.0(0)
call with kaiCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/26

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 10:34 PM on 1/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

27 Terms

1
New cards

file.read() =

What reads an entire file into ONE BIG STRING?

2
New cards

file.readline()

What reads and returns the next line, returns empty string at end of file.

3
New cards

file.readlines()

What reads the entire file into a list of strings

4
New cards

What do each of readlines, readline, and read leave at the end of the long string?

\n automatically

5
New cards

.split()

Breaks into a list of single characters, can take an argument of separator or maxsplit (max number of splits done on a string)

6
New cards

Write a function to read while using a while look

file = open

line = file.readline()

while line in lines.....

function body

file.close()

7
New cards

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()

8
New cards

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

9
New cards

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

10
New cards

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

11
New cards

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

12
New cards

If there is a number, what to do before using the ________ function?

Convert to string before using write function for files

13
New cards

PURPOSE of text files in python?

Permanent / persistent data storage

14
New cards

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

15
New cards

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()

16
New cards

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.

17
New cards

Write an alternative function to open a file

with open(file_path, mode = "r") as filename:

newvariable = file.read()

18
New cards

Write a FOR LOOP to read a file

file_path = open("titleoffile.txt")

for line in file_path:

print(line.strip())

19
New cards

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()

20
New cards

If not specified, what is the default mode for OPEN function in files?

Read mode, will not open if file does not already exist

21
New cards

List two things that makes the readline() function stop reading and printing?

1. \n new line

2. End of file

22
New cards

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

23
New cards

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

24
New cards

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.

25
New cards

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

26
New cards

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

27
New cards

BIGGEST distinction between readline and read vs READLINES

Readlines returns a list of strings. THe other two only return ONE SINGLE STRING