4.4.2.Graded Quiz Reading and Writing files with Open - sj50179/IBM-Data-Science-Professional-Certificate GitHub Wiki

LATEST SUBMISSION GRADE 100%

Question 1

Consider the following text file: Example1.txt:

This is line 1

This is line 2

This is line 3

What is the output of the following lines of code?

with open("Example1.txt","r") as File1:

     file_stuff=File1.readline ()

     print(file_stuff)
  • This is line 1
  • This is line 1 This is line 2 This is line 3
  • This is line 1 This is line 2

Correct.

Question 2

Consider the file object: File1. How would you read the first line of text?

  • File1.readline ()
  • File1.readline () File1.readline ()
  • File1.read ()

Correct.

Question 3

Consider the following line of code:

with open(example1,"r") as file1:

What mode is the file object in?

  • write
  • append
  • read

Correct, the mode is set to r for read.

Question 4

Consider the following line of code:

with open(example1,"w") as file1:

What mode is the file object in?

  • read
  • write
  • append

Correct.

Question 5

What do the following lines of code do?

with open("Example.txt","a") as writefile:

  writefile.write("This is line A\n")
  writefile.write("This is line B\n")
  • Write to the file “Example.txt”
  • Append the file "Example.txt"
  • Read the file "Example.txt"

Correct.

Question 6

What do the following lines of code do?

with open("Example3.txt","w") as file1:

file1.write("This is line C\n")
  • Append the file "Example3.txt".
  • error
  • Read the file "Example3.txt".

Correct. There is no indent.