ENGR 102 Exam
Tested Questions With Revised
Correct Detailed Answers
>Latest Update>>
When we are finished with a file, we need to close it. This ensures that the file is
left in a valid condition. - ANSWER
True
A with statement can be used to open a file, execute a blo...
When we are finished with a file, we need to close it. This ensures that the file is
left in a valid condition. - ANSWER True
A with statement can be used to open a file, execute a block of statements, and
automatically close the file when complete.
with <open command> as <fileID>: - ANSWER True
With <open command> as <fileID>:
The fileID variable can be used to refer to the file within the indented portion of
the code. - ANSWER True
Separate open and close commands are best for when you will have multiple files
open at once or for a long time. - ANSWER True
Separate open and close commands are best for when the open/close commands
are not nested:
e.g. Open 1 => Open 2 => Close 1 => Close 2 - ANSWER True
, Using the with...as formulation is better for ensuring that your file is always
closed correctly. - ANSWER True
Using the with...as formulation is better for clearly delineating which part of the
code is doing file operations (indented block of code). - ANSWER True
To write, we will use the write command:
<fileID>.write(<string to write>)
Where the fileID is the file identifier (variable name) for a file that was opened for
writing or appending. - ANSWER True
Write will write only a single string - ANSWER True
Write will write strings, floats, integers, etc. - ANSWER False
Write will put a carriage return/new line after writing - ANSWER False
When writing to a file, the mode of the file must be explicitly set in the open()
function call. - ANSWER True
The mode 'w' opens a file for writing. If the file is missing, then a new file is
created. Contents of any existing file are overwritten. - ANSWER True
Assume we have a text file open for reading and the variable myfile is the file
identifier.
,next_line = myfile.readline()
This line of code reads one line of the file at a time. - ANSWER True
We can use a version of the for loop to iterate through lines in the file.
for line in myfile:
#Do stuff with the string line - ANSWER True
<string variable> = <fileID>.read()
Will read the entire file into one single string - ANSWER True
Both of these will convert all the lines in the file into a list of strings. - ANSWER
True
The split method will convert a string into a list of strings, based on a separator
that is specified. - ANSWER True
The join() string method performs the inverse operation of split(). It joins a list of
strings together to create a single string. - ANSWER True
x = "".join(['1', '2', '3'])
The type of the variable x is integer. - ANSWER False
, The write command automatically adds a newline character at the end of your
line - ANSWER False
Which of the following is the correct way to open a file in Python? Choose all that
apply.
myfile.open("log_file.csv", 'a')
myfile = open("mygrades.txt", 'r+')
In Captain James T. Kirk's voice: Computer, open file "StarTrekIsAwesome.csv"
with open("datafile.dat", 'w') as myfile: - ANSWER myfile =
open("mygrades.txt", 'r+')
with open("datafile.dat", 'w') as myfile:
What is the correct way to read from a file with the file identifier myfile? Choose
all that apply.
if myfile: read()
data = list(myfile)
for data in myfile: # do stuff
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller NURSINGDICTIONARY. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $12.99. You're not tied to anything after your purchase.