Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Question 1 Given a file, browsers.txt that looks like firefox chrome brave edge safari internet explorer opera What would be the expected output of


 


image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Question 1 Given a file, "browsers.txt" that looks like firefox chrome brave edge safari internet explorer opera What would be the expected output of the following code? with open('browsers.txt', 'r') as infile: for line in infile: if "er" in line: print(line.strip()) (*Note that /represent line breaks in the answer options.) internet explorer/opera firefox/chrome/internet explorer/opera er/er internet explorer /opera 4 pts Question 2 If you were to use the following code to write a file, cat_list = ['Siamese', 'Manx', 'Abyssinian', 'Savannah', 'Ragdoll'] with open('cats.txt', 'w') as outfile: for cat in cat_list: outfile.write(cat + ' ') what would happen if 'cats.txt' already exists? The contents of cat_list would merge with the cats.txt contents in unpredictable ways, potentially creating an unreadable file. The cats.txt file would be modified, and the items in cat_list would be added to it. The cats.txt would be overwritten (i.e. the contents would be replaced). The program would pause and a message would pop up that says, "Are you sure you want to overwrite 'cats.txt'? This action cannot be undone." Question 3 Which of these is a main difference between text and binary files? Binary files are always two times as large as text files, no matter what is in them. Text files are composed with characters and are (usually) readable by humans, while binary files are (usually) not. Binary files can only be opened once, while text files can be opened multiple times. Text files are not readable by humans because they are composed of bits, but binary files usually are. 4 pts 4 pts Question 4 Say we want to write some information to a file using with open('stuff.txt', 'w') as outfile: for thing in things: outfile.write(thing + ' ') What type can each thing item be? Int or float only Any iterable type String, int, float, bool String only Question 5 Identify the terms in the following line below: infile = open('dogs.txt', 'r') open 'dogs.txt' 'r' [Choose ] An argument to open() that tells it to read the file. The Python function that reads a file An argument to open() that tells it the name of the file to read. The name of the variable to which the iterable output of open() is assigned. [Choose ] infile [Choose ] 4 pts 4 pts Question 6 4 pts Say you want to write a program that reads text output from a file called "sat_data.txt". You want your program to end gracefully without errors if the file doesn't exist. What can you do to accommodate this uncertainty? Use open('log.txt', 'r') and hope for the best. Use open('log.txt', 'r') with a try...except to handle the exception if the file does not already exist Use open('log.txt', 'w') Use open('log.txt', 'a')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Lets break down each question and determine the correct answers Question 1 The code provided reads e... blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

More Books

Students explore these related Programming questions

Question

Define Marketing research.

Answered: 3 weeks ago