PythonII Notes Unit 1
PythonII Notes Unit 1
Python has in-built functions to create and manipulate files. The io module is the default
module for accessing files and you don't need to import it. The module consists of
open(filename, access_mode) that returns a file object, which is called "handle" . You
can use this handle to read from or write to a file.
Python treats the file as an object, which has its own attributes and methods.
1. Text files have an End-Of-Line (EOL) character to indicate each line's termination.
In Python, the new line character (\n) is default EOL terminator.
2. Since binary files store data after converting it into binary language (0s and 1s),
there is no EOL character. This file type returns bytes. This is the file to be used
when dealing with non-text files such as images or exe.
Open()
The two important arguments are file and mode. ‘file’ is the argument that should be
mandatorily passed as for all other arguments there are default values given.
1. file
This argument is basically the path where your file resides.If the path is in current
working directory, you can just provide the filename, like
my_file_handle=open("mynewtextfile.txt")
If the file resides in a directory other than that, you must provide the full path with the
file name:
my_file_handle=open("D:\\new_dir\\anotherfile.txt")
my_file_handle.read()
Make sure file name and path given is correct, otherwise you'll get a
FileNotFoundError.
Access modes define in which way you want to open a file, you want to open a file
for read only, write only or for both. It specifies from where you want to start reading
or writing in the file.You specify the access mode of a
file through the mode argument. You use 'r' , the default mode, to read the
file. In other cases where you want to write or append, you use , 'w' or 'a'
respectively.
Character Function
Open file for reading only. Starts reading from beginning of file.
r This default mode.
Open a file for reading only in binary format. Starts reading from
rb beginning of file.
Open file for reading and writing. File pointer placed at beginning of
r+ the file.
Character Function
of the file. Overwrites existing file and creates a new one if it does
not exists.
Open a file for appending. Starts writing at the end of file. Creates a
a new file if file does not exist.
Same as a but in binary format. Creates a new file if file does not
ab exist.
close() function
Every opened file needs to be closed for clean up, which is achieved by making a close()
call on file object.
i.e file1.close() # closes the the file associated and frees the memory space acquired by
that file.
with statement in Python is used in exception handling to make the code cleaner and
much more readable. In this case no explicit close() function is called , file is cleaned up
automatically when we leave the ‘with ‘ block.
e.g:
• read([n])
• readline([n])
• readlines()
writelines() : For a list of string elements, each string is inserted in the text file. Used to
insert multiple strings at a single time.
with open(“C:\\Data\\names.txt”,”w” ) as file1:
names=[“Sam\n”,”Lucy\n”,”Amanda\n”]
file1.writelines(names)
# writes the entire list to file as various lines
For appending to an existing file the same functions are used only the mode changes
from “w” to “a”
A directory or folder is a collection of files and sub directories. Python has the os module
which provides us with many useful methods to work with directories (and files as well).
This method returns the current working directory in the form of a string. We can also
use the getcwdb() method to get it as bytes object.
>>> import os
>>> os.getcwd()
'C:\\Program Files\\Python'
The extra backslash implies escape sequence. The print() function will render this
properly.
>>> print(os.getcwd())
C:\Program Files\Python
Changing Directory
We can change the current working directory using the chdir() method.
The new path that we want to change to must be supplied as a string to this method. We
can use both forward slash (/) or the backward slash (\) to separate path elements.
>>> os.chdir('C:\\Python33')
>>> print(os.getcwd())
C:\Python33
This method takes in a path and returns a list of sub directories and files in that path. If
no path is specified, it returns from the current working directory.
>>> print(os.getcwd())
C:\Python33
>>> os.listdir()
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']
>>> os.listdir('G:\\')
['$RECYCLE.BIN',
'Movies', 'Music',
'Photos',
'Series',
'System Volume Information']
This method takes in the path of the new directory. If the full path is not specified, the
new directory is created in the current working directory.
>>> os.mkdir('test')
>>> os.listdir()
['test']
The first argument is the old name and the new name must be supplies as the second
argument.
>>> os.listdir()
['test']
>>> os.rename('test','new_one')
>>> os.listdir()
['new_one']
>>> os.listdir()
['new_one', 'old.txt']
>>> os.remove('old.txt')
>>> os.listdir()
However, note that rmdir() method can only remove empty directories.
In order to remove a non-empty directory we can use the rmtree() method inside the
shutil module.
>>> os.listdir()
['test']
>>> os.rmdir('test')
Traceback (most recent call last):
...
OSError: [WinError 145] The directory is not empty: 'test'
>>> import shutil
>>> shutil.rmtree('test')
>>> os.listdir()
[]
What is Exception?
An exception is an event, which occurs during the execution of a program that disrupts
the normal flow of the program's instructions. In general, when a Python script encounters
a situation that it cannot cope with, it raises an exception. An exception is a Python object
that represents an error.
When a Python script raises an exception, it must either handle the exception immediately
otherwise it terminates and quits.
Syntax
Here is simple syntax of try....except...else blocks −
ExceptionI:
else:
try block contains statements that may throw different types of exceptions.
• You can also provide a generic except clause, which handles any exception.
• After the except clause(s), you can include an else-clause. The code in the else-
block executes if the code in the try: block does not raise an exception.
• The else-block is a good place for code that does not need the try: block's
protection.
try:
fh = open("testfile", "w")
except IOError:
fh.close()
fh = open("testfile", "r")
except IOError:
except:
else:
finally:
fh = open("testfile", "w")
finally:
fh.close()
except IOError:
When an exception is thrown in the try block, the execution immediately passes to the
finally block. After all the statements in the finally block are executed, the exception is
raised again and is handled in the except statements if present in the next higher layer of
the try-except statement.
Here, Exception is the type of exception (for example, NameError) and argument is a
value for the exception argument. The argument is optional; if not supplied, the
exception argument is None.
The final argument, traceback, is also optional (and rarely used in practice), and if present,
is the traceback object used for the exception.
def functionName( x,y ):
if y==0:
raise Exception(y)
# The code below to this would not be executed
# if we raise the exception
return x/y
try:
l = functionName(3,0)
except Exception as e:
Instead of waiting for a program to crash midway, you can also start by making an assertion
in Python. We assert that a certain condition is met. If this condition turns out to be True,
then that is excellent! The program can continue. If the condition turns out to be False, you
can have the program throw an AssertionError exception.
a=int(input("Enter a number"))
print(a)
An object is called iterable if we can get an iterator from it. Most of built-in containers
in Python like: list, tuple, string etc. are iterables.
The iter() function (which in turn calls the __iter__() method) returns an iterator from
them.
4
7
0
3
A more elegant way of automatically iterating is by using the for loop. Using this, we
can iterate over any object that can return an iterator, for example list, string, file etc. A
for loop as given below
is actually implemented as
# create an iterator object from that iterable
iter_obj = iter(iterable)
# infinite loop
while True:
try:
# get the next item
element = next(iter_obj)
# do something with element
except StopIteration:
# if StopIteration is raised, break from loop
break
So internally, the for loop creates an iterator object, iter_obj by calling iter() on the
iterable.
Metacharacters
1. .(dot)- Matches any single character (many applications exclude newlines Within
POSIX bracket expressions, the dot character matches a literal dot. For example,
a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".
2. +- Matches the preceding element one or more times. For example, ab+c matches
"abc", "abbc", "abbbc", and so on, but not "ac"
3. ?- Matches the preceding pattern element zero or one times
4. *- Matches the preceding element zero or more times. For example, ab*c matches
"ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy",
and so on. (ab)* matches "", "ab", "abab", "ababab", and so on.
5. ^- Matches the beginning of a line or string.
6. $- Matches the end of a line or string.
7. [ ]- A bracket expression. Matches a single character that is contained within the
brackets. For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which
matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z]
matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].
8. [^ ]- Matches a single character that is not contained within the brackets. For
example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches
any single character that is not a lowercase letter from "a" to "z". Likewise, literal
characters and ranges can be mixed.
9. ()-Defines a marked subexpression
10. {m,n} Matches the preceding element at least m and not more than n times.
For example, a{3,5} matches only "aaa", "aaaa", and "aaaaa".
11. {m}-Matches the preceding element exactly m times
There are some special sequences beginning with '\' represent predefined sets of
characters that are often useful, such as the set of digits, the set of letters, or the set
of anything that isn’t whitespace.
The re module provides an interface to the regular expression engine, allowing you to
compile REs into objects and then perform matches with them.
To replace all matching portions with something else can be done using the
re.sub()method. Below, we are finding all vowel sequences and replacing them with '-'.
The method returns the result as a new string.
>>> wood
'How much wood would a woodchuck chuck if a woodchuck could chuck wood?'
'H-w m-ch w-d w-ld - w-dch-ck ch-ck -f - w-dch-ck c-ld ch-ck w-d?'
Removing the matching portions can also be achieved through re.sub(): just make the
"replacer" string an empty string ''.
>>> re.sub(r'[aeiou]+', '', wood) # substitute with an empty string
'Hw mch wd wld wdchck chck f wdchck cld chck wd?'
There are two functions for this. The match() and the search() function returns a match
object on successful matchand returns None if no match is found. The match() function
only checks if the RE matches at the beginning of the string while search() will scan
forward through the string for a match. It’s important to keep this distinction in mind.
Remember, match() will only report a successful match which will start at 0; if the match
wouldn’t start at zero,match() will not report it.
>>> print(re.match('super', 'superstition').span())
(0, 5)
>>> print(re.match('super', 'insuperable'))
None
Here the span() function on match object return a tuple containing the (start, end)
positions of the match.Other methods are
On the other hand, search() will scan forward through the string, reporting the first
match it finds.
Compilation flags let you modify some aspects of how regular expressions work.
Flag Meaning
Makes several escapes like \w, \b, \s and \d match only on ASCII
ASCII, A
characters with the respective property.