Unit-Iii Chapter-1: Python Strings Revisited
Unit-Iii Chapter-1: Python Strings Revisited
Indexing: Individual characters in a string are accessed using the subscript ([ ]) operator. The expression in brackets
is called an index. The index specifies a member of an ordered set and in this case it specifies the character we want to
access from the given set of characters in the string.
The index of the first character is 0 and that of the last character is n-1 where n is the number of characters in the
string. If you try to exceed the bounds (below 0 or above n-1), then an error is raised.
2
Example:
Example:
Examples:
10
Examples:
11
12
13
Examples:
14
15
Examples:
16
17
18
Example:
19
20
Examples:
21
23
24
Examples:
25
Example:
The content of groups in a match can be accessed by using the group() function. For example,
•group(0) or group() returns the whole match.
•group(n), where n is greater than 0, returns the nth group from the left. 26
•group() returns all groups up from 1. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
UNIT-III CHAPTER-2
File Handling
27
A file is a collection of data stored on a secondary storage device like hard disk.
A file is basically used because real-life applications involve large amounts of data and in such situations the console
• First, it becomes cumbersome and time consuming to handle huge amount of data through terminals.
• Second, when doing I/O using terminal, the entire data is lost when either the program is terminated or computer is
turned off. Therefore, it becomes necessary to store data on a permanent storage (the disks) and read whenever
28
A binary file is a file which may contain any type of data, encoded in binary form for computer storage and processing
purposes. It includes files such as word processing documents, PDFs, images, spreadsheets, videos, zip files and other
executable programs. Like a text file, a binary file is a collection of bytes. A binary file is also referred to as a character
stream with following two essential differences.
• A binary file does not require any special processing of the data and each byte of data is transferred to or from the
disk unprocessed.
• Python places no constructs on the file, and it may be read from, or written to, in any manner the programmer
wants.
While text files can be processed sequentially, binary files, on the other hand, can be either processed sequentially or
randomly depending on the needs of the application.
31
Before reading from or writing to a file, you must first open it using Python’s built-in open() function. This function
creates a file object, which will be used to invoke methods associated with it. The syntax of open() is:
fileObj = open(file_name [, access_mode])
Here,
file_name is a string value that specifies name of the file that you want to access.
access_mode indicates the mode in which the file has to be opened, i.e., read, write, append, etc.
Example:
32
33
Example:
34
Examples:
36
Example:
37
Example:
38
Examples
:
39
Example:
40
41
43
The os module in Python has various methods that can be used to perform file-processing operations like renaming and
deleting files. To use the methods defined in the os module, you should first import it in your program then call any
related functions.
The rename() Method: The rename() method takes two arguments, the current filename and the new filename. Its
syntax is: os.rename(old_file_name, new_file_name)
The remove() Method: This method can be used to delete file(s). The method takes a filename (name of the file to be
deleted) as an argument and deletes that file. Its syntax is: os.remove(file_name)
Examples
:
44
The makedirs() method: The method mkdirs() is used to create more than one folder. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Directory Methods - Examples
46
The os.path.abspath() method uses the string value passed to it to form an absolute path. Thus, it is another way to
convert a relative path to an absolute path
The os.path.isabs(path) method accepts a file path as an argument and returns True if the path is an absolute path and
False otherwise.
The os.path.relpath(path, start) method accepts a file path and a start string as an argument and returns a relative
path that begins from the start. If start is not given, the current directory is taken as start.
The os.path.dirname(path) Method returns a string that includes everything specified in the path (passed as argument
to the method) that comes before the last slash.
The os.path.basename(path) Method returns a string that includes everything specified in the path (passed as argument
to the method) that comes after the last slash.
47
The os.path.split(path) Method: This method accepts a file path and returns its directory name as well as the . So it is
equivalent to using two separate methods os.path.dirname() and os.path.basename()
The os.path.getsize(path) Method: This method returns the size of the file specified in the path argument.
The os.listdir(path) Method: The method returns a list of filenames in the specified path.
The os.path.exists(path) Method: The method as the name suggests accepts a path as an argument and returns True if
the file or folder specified in the path exists and False otherwise.
The os.path.isfile(path) Method: The method as the name suggests accepts a path as an argument and returns True if
the path specifies a file and False otherwise.
The os.path.isdir(path) Method: The method as the name suggests accepts a path as an argument and returns True if
the path specifies a folder and False otherwise.
48
49