Home | | Problem Solving and Python Programming | Python Files, Modules, Packages: short important questions and answers

Problem Solving and Python Programming - Python Files, Modules, Packages: short important questions and answers | Problem Solving and Python Programming : Files, Modules, Packages

Chapter: Problem Solving and Python Programming : Files, Modules, Packages

Python Files, Modules, Packages: short important questions and answers

Problem Solving and Python Programming : Files, Modules, Packages - important questions and answers

FILES, MODULES AND PACKAGES

 

1. What is a text file?

 

A text file is a file that contains printable characters and whitespace, organized in to lines separated by newline characters.

 

2. Write a python program that writes “Hello world” into a file.

f =open("ex88.txt",'w')

f.write("hello world")

f.close()

 

3. Write a python program that counts the number of words in a file. f=open("test.txt","r")

content =f.readline(20) words =content.split()

print(words)

 

4. What are the two arguments taken by the open() function?

The open function takes two arguments : name of the file and the mode of operation.

Example: f = open("test.dat","w")

 

5. What is a file object?

A file object allows us to use, access and manipulate all the user accessible files. It maintains the state about the file it has opened.

 

6. What information is displayed if we print a file object in the given program? f= open("test.txt","w")

print f

The name of the file, mode and the location of the object will be displayed.

 

7. What is an exception?

Whenever a runtime error occurs, it creates an exception. The program stops execution and prints an error message. For example, dividing by zero creates an exception:

print 55/0

ZeroDivisionError: integer division or modulo

 

8. What are the error messages that are displayed for the following exceptions?

a.              Accessing a non-existent list item

b.              Accessing a key that isn’t in the dictionary

c.               Trying to open a non-existent file

a.              IndexError: list index out of range

b.              KeyError: what

c.               IOError: [Errno 2] No such file or directory: 'filename'

 

9. What are the two parts in an error message?

The error message has two parts: the type of error before the colon, and speci_cs about the error after the colon.

 

10. How do you handle the exception inside a program when you try to open a non-existent file?

filename = raw_input('Enter a file name: ')

try:

f = open (filename, "r")

except IOError:

print 'There is no file named', filename

 

11. How does try and execute work?

 

The try statement executes the statements in the first block. If no exception occurs, then except statement is ignored. If an exception of type IOError occurs, it executes the statements in the except branch and then continues.

 

12. What is the function of raise statement? What are its two arguments?

The raise statement is used to raise an exception when the program detects an error. It takes two arguments: the exception type and specific information about the error.

 

13. What is a pickle?

Pickling saves an object to a file for later retrieval. The pickle module helps to translate almost any type of object to a string suitable for storage in a database and then translate the strings back in to objects.

 

14. What are the two methods used in pickling?

The two methods used in pickling are pickle.dump() and pickle.load(). To store a data structure, dump method is used and to load the data structures that are dumped , load method is used.

 

15. What is the use of the format operator?

The format operator % takes a format string and a tuple of expressions and yields a string that includes the expressions, formatted according to the format string.

 

16. What are modules?

A module is simply a file that defines one or more related functions grouped together. To reuse the functions of a given module, we need to import the module. Syntax: import <modulename>

 

17.  What is a package?

Packages are namespaces that contain multiple packages and modules themselves. They are simply directories.

Syntax: from <mypackage> import <modulename>

 

18. What is the special file that each package in Python must contain? Each package in Python must contain a special file called __init__.py

 

19. How do you delete a file in Python?

The remove() method is used to delete the files by supplying the name of the file to be deleted as argument.

Syntax: os.remove(filename)

 

20.  How do you use command line arguments to give input to the program?

Python sys module provides access to any command-line arguments via sys.argv. sys.argv is the list of command-line arguments. len(sys.argv) is the number of command-line arguments.

 

Tags : Problem Solving and Python Programming , Problem Solving and Python Programming : Files, Modules, Packages
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Files, Modules, Packages : Python Files, Modules, Packages: short important questions and answers | Problem Solving and Python Programming


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.