Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Join us on Telegram

Home Forums Assignment courserra Python for Everybody – Specialization Python Data Structures Week 3, Chapter 7, Quiz, Python for Data Structure

Viewing 0 reply threads
  • Author
    Posts
    • #821
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      1.Given the architecture and terminology we introduced in Chapter 1, where are files stored?

        Asnwer-

      Secondary memory

      2.What is stored in a “file handle” that is returned from a successful open() call?

        Answer-

      The handle is a connection to the file's data

      3.What do we use the second parameter of the open() call to indicate?

        Answer-

      Whether we want to read data from the file or write data to the file

      4.What Python function would you use if you wanted to prompt the user for a file name to open?

        Answer-

      input()

      5.What is the purpose of the newline character in text files?

        Answer-

      It indicates the end of one line of text and the beginning of another line of text.

      6.If we open a file as follows: xfile = open(‘mbox.txt’). What statement would we use to read the file one line at a time?

        Answer-

      for line in xfile:

      7.What is the purpose of the following Python code?

      fhand = open('mbox.txt'); 
      x = 0; 
      for line in fhand: 
         x = x + 1; 
      print x
        Answer-

      Count the lines in the file 'mbox.txt'

      8.If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem?.
      From: stephen.marquard@uct.ac.za;
      From: louis@media.berkeley.edu;
      From: zqian@umich.edu;
      From: rjlowe@iupui.edu

        Answer-

      rstrip()

      9.The following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered?

      fname = raw_input('Enter the file name: '); 
      fhand = open(fname)
        Answer-

      ==> try / except

      10.What does the following Python code do?

      fhand = open('mbox-short.txt'); 
      inp = fhand.read()
        Answer-

      Reads the entire file into the variable inp as a string

Viewing 0 reply threads
  • You must be logged in to reply to this topic.