Unit 6 1
Unit 6 1
Python provides two built-in functions to read a line of text from standard
input, which by default comes from the keyboard. These functions are −
raw_input
input
The raw_input([prompt]) function reads one line from standard input and
returns it as a string.
>>> str = raw_input("Enter your input: ")
Enter your input: mmpolytechnic
>>> print(str)
mmpolytechnic
>>>
Python has several functions for creating, reading, updating, and deleting files.
The key function for working with files in Python is the open() function.
"r" - Read - Default value. Opens a file for reading, error if the file does not
exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
Assume we have the following file, located in the same folder as Python:
Read Only Parts of the File
By default the read() method returns the whole text, but you can also specify
how many characters you want to return:
Readline
Readlines
Read and return a list of lines from the file. Reads in at most n bytes/
characters if specified.
Reading and writing files
This method starts reading from the beginning of the file and if count is
missing, then it tries to read as much as possible, maybe until the end of file.
Closing a file
To use this module you need to import it first and then call any related
functions.
The rename() method takes two arguments, the current filename and the new
filename.
The remove() Method
You can use the remove() method to delete files by supplying the name of the
file to be deleted as the argument.
Python provides us with the way to handle the Exception so that the other
part of the code can be executed without any disruption. However, if we do
not handle the exception, the interpreter doesn't execute all the code that
exists after that.
Common Exceptions
EOFError: It occurs when the end of the file is reached, and yet operations are
being performed.
The try block lets you test a block of code for errors.
The except block lets you handle the error.
The finally block lets you execute code, regardless of the result of the
try- and except blocks.
When an error occurs, or exception as we call it, Python will normally stop
and generate an error message.
except Exception1:
#block of code
except Exception2:
#block of code
#other code
The use the else statement with the try-except statement, place the code
which will be executed in the scenario if no exception occurs in the try block.
The syntax to use the else statement with the try-except statement is given
below.
If file not present:
If file present:
Python has many built-in exceptions which forces program to output an error
when something in it goes wrong.
In Python, users can define such exceptions by creating a new class. This
exception class has to be derived, either directly or indirectly, from Exception
class.