0% found this document useful (0 votes)
11 views

Module 3 Notes

Uploaded by

poojarideeksha16
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 3 Notes

Uploaded by

poojarideeksha16
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Introduction to

Python
Programming

MAY 20

SMVITM, Bantakal
by: Hithesh

1
Module 3: Manipulating Strings
Manipulating Strings: Working with Strings, Useful String Methods, Project:
Password Locker, Project: Adding Bullets to Wiki Markup

Reading and Writing Files: Files and File Paths, The os.path Module, The File
Reading/Writing.

Process, Saving Variables with the shelve Module, Saving Variables with the
print.format() Function, Project: Generating Random Quiz Files, Project:
Multiclipboard.

2
Working with Strings
String Literals
➢ Typing string values in Python code is fairly straightforward: They begin and end
with a single quote.
➢ But there is an issue to use a quote inside a string,
➢ Typing 'That is Alice's cat.' won’t work, because Python thinks the string ends
after Alice, and the rest (s cat.') is invalid Python code.
➢ Fortunately, there are multiple ways to type strings.

Double Quotes
➢ Strings can begin and end with double quotes, just as they do with single quotes.
➢ One benefit of using double quotes is that the string can have a single quote
character in it.
Enter the following into the interactive shell

>>> spam = "That is Alice's cat."

➢ Since the string begins with a double quote, Python knows that the single
quote is part of the string and not marking the end of the string.
➢ However, if you need to use both single quotes and double quotes in the
string, you’ll need to use escape characters.

Escape Characters
➢ An escape character lets you use characters that are otherwise impossible to put
into a string.
➢ An escape character consists of a backslash (\) followed by the character you
want to add to the string. (Despite consisting of two characters, it is commonly
referred to as a singular escape character.)
➢ For example, the escape character for a single quote is \'.
➢ You can use this inside a string that begins and ends with single quotes.
Enter the following into the interactive shell:

3
>>> spam = 'Say hi to Bob\'s mother.'

Python knows that since the single quote in Bob\'s has a backslash, it is not a single
quote meant to end the string value.
The escape characters \' and \" let you put single quotes and double quotes inside
your strings, respectively.

Enter the following into the interactive shell:

>>> print("Hello there!\nHow are you?\nI\'m doing fine.")


Hello there!
How are you?
I'm doing fine.
Raw Strings
➢ You can place an r before the beginning quotation mark of a string to make it a
raw string.
➢ A raw string completely ignores all escape characters and prints any backslash
that appears in the string.
For example, type the following into the interactive shell:

>>>print(r'That is Carol\'s cat.')


That is Carol\'s cat.

4
➢ Because this is a raw string, Python considers the backslash as part of the string
and not as the start of an escape character.
➢ Raw strings are helpful if you are typing string values that contain many
backslashes.

Multiline Strings with Triple Quotes


➢ While you can use the \n escape character to put a newline into a string, it is
often easier to use multiline strings.
➢ A multiline string in Python begins and ends with either three single quotes or
three double quotes.
➢ Any quotes, tabs, or newlines in between the “triple quotes” are considered
part of the string.
➢ Python’s indentation rules for blocks do not apply to lines inside a multiline
string.
Open the file editor and write the following:
print('''Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob''')
Output:
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
Notice that the single quote character in Eve's does not need to be escaped.
Escaping single and double quotes is optional in raw strings.
The following print () call would print identical text but doesn’t use a multiline string:

print('Dear Alice,\n\nEve\'s cat has been arrested for catnapping, cat burglary, and
extortion.\n\nSincerely,\nBob')

5
Multiline Comments
➢ While the hash character (#) marks the beginning of a comment for the rest of
the line,
➢ a multiline string is often used for comments that span multiple lines.
The following is perfectly valid Python code

"""This is a test Python program.


Written by Al Sweigart al@inventwithpython.com
This program was designed for Python 3, not Python 2.
"""
def spam():
"""This is a multiline comment to help
explain what the s spam() function does."""
print('Hello!')

Indexing and Slicing Strings


➢ Strings use indexes and slices the same way lists do.
➢ You can think of the string 'Hello world!' as a list and each character in the string
as an item with a corresponding index.

➢ The space and exclamation point are included in the character count, so 'Hello
world!' is 12 characters long, from H at index 0 to ! at index 11.

Enter the following into the interactive shell:

>>> spam = 'Hello world!'


>>> spam[0]
'H'
>>> spam[4]
'o'
6
>>> spam[-1]
'!'
>>> spam[0:5]
'Hello'
>>> spam[:5]
'Hello'
>>> spam[6:]
'world!'

➢ If you specify an index, you’ll get the character at that position in the string.
➢ If you specify a range from one index to another, the starting index is included
and the ending index is not.
➢ That’s why, if spam is 'Hello world!', spam[0:5] is 'Hello'.
➢ The substring you get from spam[0:5] will include everything from spam[0] to
spam[4], leaving out the space at index 5.
➢ Note that slicing a string does not modify the original string.
➢ You can capture a slice from one variable in a separate variable.

Try typing the following into the interactive shell


By slicing and storing the
resulting substring in another
variable, you can have both the
whole string and the substring
handy for quick, easy access.

The in and not in Operators with Strings


➢ The in and not in operators can be used with strings just like with list values.
➢ An expression with two strings joined using in or not in will evaluate to a
Boolean True or False.
Enter the following into the interactive shell:

7
Useful String Methods

Several string methods analyze strings or create transformed string values.

The upper()
lower()
isupper() and
islower()

The upper()
➢ The upper() method returns a string where all characters are in upper case.
➢ Symbols and Numbers are ignored.

Syntax: string.upper()

8
Parameters: The upper() method doesn’t take any parameters.
Returns: returns an uppercased string of the given string.

Ex:
>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> spam
'HELLO WORLD!'

➢ Note that these methods do not change the string itself but return new string
values.
➢ If you want to change the original string, you have to call upper() on the string
and then assign the new string to the variable where the original was stored.
➢ This is why you must use spam = spam.upper() to change the string in spam
instead of simply spam.upper().
➢ (This is just like if a variable eggs contains the value 10. Writing eggs + 3 does not
change the value of eggs, but eggs = eggs + 3 does.)
➢ The upper() methods are helpful if you need to make a case-insensitive
comparison.
The lower()
Python String lower() method converts all uppercase characters in a string into
lowercase characters and returns it.
➢ Symbols and Numbers are ignored.

Syntax: string.lower()

Parameters: The lower() method doesn’t take any parameters.


Returns: returns an lowercase string of the given string.

Ex:
>>> spam = 'Hello World!'
>>> spam = spam.lower()
>>> spam
9
'hello world!'

print('How are you?')


feeling = input()
if feeling.lower() == 'great':
print('I feel great too.')
else:
print('I hope the rest of your day is good.')

output:
How are you?
GREat
I feel great too.

The isupper()
➢ The isupper() method returns True if all the characters are in upper case,
otherwise False.
➢ Symbols and Numbers are ignored.
➢ It returns “True” for whitespaces but if there is only whitespace in the string
then returns “False”.
➢ It does not take any arguments, Therefore, It returns an error if a parameter is
passed.
➢ Digits and symbols return “True” but if the string contains only digits and
numbers then returns “False”

Syntax: string.isupper()

Parameters: The isupper() method doesn’t take any parameters.


Returns: True- If all characters in the string are uppercase. False- If the string contains 1
or more non-uppercase characters.

10
Ex:
a = "Hello World!"
b = "hello 123"
c = "MY NAME IS PETER"

print(a.isupper())
print(b.isupper())
print(c.isupper())

OUTPUT:
False
False
True

The islower()
➢ The islower() method returns True if all the characters are in lower case,
otherwise False.
➢ Numbers, symbols and spaces are not checked, only alphabet characters.
➢ It returns “True” for whitespaces but if there is only whitespace in the string
then returns “False”.
➢ It does not take any arguments; Therefore, it returns an error if a parameter is
passed.
➢ Digits and symbols return “True” but if the string contains only digits and
numbers then returns “False”.

Syntax: string.islower()

Parameters: The islower() method doesn’t take any parameters.


Returns: True- If all characters in the string are lowercase. False- If the string contains 1
or more non-lowercase characters.

11
Ex:
a = "Hello World!"
b = "hello 123"
c = "my name is peter"

print(a.islower())
print(a.islower())
print(a.islower())

OUTPUT:
False
False
True

The isX String Methods


➢ Along with islower() and isupper(), there are several string methods that have
names beginning with the word is.
➢ These methods return a Boolean value that describes the nature of the string.
Here are some common isX string methods:
➢ isalpha() returns True if the string consists only of letters and is not blank.
12
➢ isalnum() returns True if the string consists only of letters and numbers and is
not blank.
➢ isdecimal() returns True if the string consists only of numeric characters and is
not blank
➢ isspace() returns True if the string consists only of spaces, tabs, and newlines
and is not blank.
➢ istitle() returns True if the string consists only of words that begin with an
uppercase letter followed by only lowercase letters.

The startswith() and endswith() String Methods

Startswith()
The startswith() method returns True if the string starts with the specified value,
otherwise False.

13
Syntax: string_name.startswith(search_string, start, end)

Parameters:
search_string: Required.The string to be searched.
start: Optional. start index of the str from where the search_string is to be searched.
end: Optional. end index of the str, which is to be considered for searching.

Returns :
The return value is boolean.
The functions return True if the original Sentence starts with the search_string else
False

Ex 1:
message = 'Python is fun'
# checks if the message starts with Python
print(message.startswith('Python'))

Output:
True
Ex 2:

message = 'Python is fun'


# checks if the message starts with Python
print(message.startswith('fun'))

Output:
False

endswith()

➢ endswith() function is used to check whether a given Sentence ends with


some particular string.

➢ Start and end parameter are optional.

14
➢ We may use them when we want only some particular substring of the
original string to be considered for searching.

Syntax : string_name.endswith( search_string, start, end)

Parameters :

search_string : Required. The string to be searched.


start : Optional.Start index of the str from where the search_string is to be
searched.

end : Optional. End index of the str, which is to be considered for searching.
Returns :
The return value is boolean. The functions returns True if the original Sentence
ends with the search_string else False.

Ex:
message = 'Python is fun'
# check if the message starts with Python
print(message.endswith('fun'))

Output:
True

Ex:
message = 'Python is fun'
# check if the message starts with Python
print(message.endswith('Fun'))

Output:
False

15
The join() and split() String Methods
➢ The join() method is useful when you have a list of strings that need to be
joined together into a single string value.
➢ The join() method takes all items in an iterable and joins them into one
string.
➢ A string must be specified as the separator.

Syntax:
string.join(iterable)

Example
x = ['cats', 'rats', 'bats']

xj = ', '.join(x)

print(xj)

Output:
cats, rats, bats

Example

myTuple = ("John", "Peter", "Vicky")


x = "#" .join(myTuple)

print(x)

Output:

John#Peter#Vicky
16
Python String split() Method
The split() method splits a string into a list.
You can specify the separator, default separator is any whitespace.

Note: When maxsplit is specified, the list will contain the specified number of
elements plus one.

Syntax: string.split(separator, maxsplit)

Ex:

txt = "hello, my name, is Peter I am 26 years old"


x = txt.split(", ")
print(x)

print(type(x))

Output:

['hello', 'my name', 'is Peter I am 26 years old']


<class 'list'>

Ex

txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter to 1, will return a list with 2 elements!

x = txt.split("#", 1)

print(x)

Output:
['apple', 'banana#cherry#orange']

<class 'list'>
17
Justifying Text with rjust(), ljust(), and center()
Python String rjust() Method – right justify
The rjust() method will right align the string, using a specified character (space
is default) as the fill character.

Syntax

string.rjust(length, character)

length → Required. The length of the returned string

character→ Optional. A character to fill the missing space (to the left of the
string).
Default is " " (space).

Ex.

txt = "banana"
x = txt.rjust(20) # Return a 20 characters long, right justified version of the
word "banana":

print(x, "is my favorite fruit.")

Output:

banana is my favorite fruit.


Ex.

txt = "banana"

x = txt.rjust(20, “0”)
print(x)

Output:

OOOOOOOOOOOOOObanana
18
Python String ljust() Method – left justify
The ljust() method will left align the string, using a specified character (space is
default) as the fill character.

Syntax
string.ljust(length, character)
length → Required. The length of the returned string

character → Optional. A character to fill the missing space (to the right of the
string).
Default is " " (space).

Ex:
txt = "banana"

x = txt.ljust(20,'-')
print(x, "is my favorite fruit.")

Output:

banana-------------- is my favorite fruit.

Ex:

txt = "banana"

x = txt.ljust(20, "O")

print(x)

Output:
19
bananaOOOOOOOOOOOOOO
Python String center() Method

The center() method will center align the string, using a specified character (space is
default) as the fill character.

Syntax
string.center(length, character)

length → Required. The length of the returned string


character → Optional. The character to fill the missing space on each side. Default is " "
(space)

Ex:
txt = "banana"
x = txt.center(20, "O")
print(x)

Output:
OOOOOOObananaOOOOOOO

Removing Whitespace with strip(), rstrip(), and lstrip()

Python String strip() Method:

The strip () method removes any leading, and trailing whitespaces.


Leading means at the beginning of the string, trailing means at the end.
You can specify which character(s) to remove, if not, any whitespaces will be removed.

Syntax
string.strip(characters)

characters → Optional. A set of characters to remove as leading/trailing characters.


20
Ex:

txt = " banana "


x = txt.strip()
print("of all fruits", x, "is my favorite")

Output:
of all fruits banana is my favorite

Ex:
txt = ",,,,,rrttgg.....banana....rrr"
x = txt.strip(",.grt")
print(x)

Output:
banana

Python String rstrip() Method

The rstrip() method removes any trailing characters (characters at the end a string),
space is the default trailing character to remove.

Syntax:
string.rstrip(characters)
characters → Optional. A set of characters to remove as trailing characters.
Ex:
txt = " banana "
x = txt.rstrip() #Removes any white spaces at the end of the string:
print("of all the fruits", x, "is my favorite")
Output:
of all the fruits banana is my favorite

21
Ex:
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw") #Removes the trailing characters
print(x)

Output:
banana

Python String lstrip() Method


The lstrip() method removes any leading characters (space is the default leading
character to remove)
Syntax
string.lstrip(characters)
characters → Optional. A set of characters to remove as leading characters.

Ex:
txt = " banana "
x = txt.lstrip()
print("of all the fruits", x, "is my favorite")

Output:
of all fruits banana is my favorite

Ex
txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)

Output:
banana

22
Copying and Pasting Strings with the pyperclip Module¶

➢ The pyperclip module has copy() and paste() functions that can send text to and
receive text from your computer’s clipboard.
➢ Sending the output of your program to the clipboard will make it easy to paste it
to an email, word processor, or some other software.
➢ Note : Copy function will convert every data type to string.
Ex:

23
Project: Adding Bullets to Wiki Markup
# Adds Wikipedia bullet points to the start of each line of text on the clipboard.

import pyperclip

pyperclip.copy('Lists of animals\nLists of aquarium life\nLists of biologists by author


abbreviation\nLists of cultivars')
text = pyperclip.paste()

# Separate lines and add stars.


lines = text.split('\n')
for i in range(len(lines)): # loop through all indexes for "lines" list
lines[i] = '* ' + lines[i] # add star to each string in "lines" list
text = '\n'.join(lines)
print(text)

OUTPUT:
* Lists of animals
* Lists of aquarium life
* Lists of biologists by author abbreviation

24
* Lists of cultivars

Step 1: Copy and Paste from the Clipboard


1. Paste text from the clipboard
2. Do something to it
3. Copy the new text to the clipboard
First step is simple here we need to copy the content and paste it in the clipboard using
following command pyperclip.copy('Lists of animals\nLists of aquarium life\nLists of
biologists by author abbreviation\nLists of cultivars')
text = pyperclip.paste()
That second step is a little tricky, but steps 1 and 3 are pretty straightforward: They
just
involve the pyperclip.copy() and pyperclip.paste() functions.
Step 2: Separate the Lines of Text and Add the Star
➢ The call to pyperclip.paste() returns all the text on the clipboard as one big string.
➢ the string stored in text would look like this: 'Lists of animals\nLists of aquarium
life\nLists of biologists by author abbreviation\nLists of cultivars'
➢ The \n newline characters in this string cause it to be displayed with multiple lines
when it is printed or pasted from the clipboard.
➢ There are many “lines” in this one string value.
➢ You want to add a star to the start of each of these lines.
➢ the split() method return a list of strings, one for each line in the original string, and
then add the star to the front of each string in the list.
text = 'Lists of animals\nLists of aquarium life\nLists of biologists by author
abbreviation\nLists of cultivars'
print(text.split('\n'))
output:
['Lists of animals', 'Lists of aquarium life', 'Lists of biologists by
author abbreviation', 'Lists of cultivars']
➢ We split the text along its newlines to get a list in which each item is one line of the
text.
➢ We store the list in lines and then loop through the items in lines.
➢ For each line, we add a star and a space to the start of the line.
25
➢ Now each string in lines begins with a star
Step 3: Join the Modified Lines
➢ The lines list now contains modified lines that start with stars.
➢ But pyperclip.copy() is expecting a single string value, not a list of string values.
➢ To make this single string value, pass lines into the join() method to get a single
string
joined from the list’s strings.
When this program is run, it replaces the text on the clipboard with text that has stars
at the
start of each line.
➢ Now the program is complete, and you can try running it with text copied to the
clipboard.
➢ Even if you don’t need to automate this specific task, you might want to automate
some other kind of text manipulation, such as removing trailing spaces from the end
of lines or converting text to uppercase or lowercase.
➢ Whatever your needs, you can use the clipboard for input and output

26
Reading and Writing Files
Files and File Paths
➢ A file has two key properties: a filename (usually written as one word) and a
path.
➢ The path specifies the location of a file on the computer.
➢ For example, there is a file on my Windows 7 laptop with the filename
projects.docx in the path C:\Users\asweigart\Documents.
➢ The part of the filename after the last period is called the file’s extension and
tells you a file’s type.
➢ project.docx is a Word document, and Users, asweigart, and Documents all
refer to folders (also called directories).
➢ Folders can contain files and other folders.
➢ For example, project.docx is in the Documents folder, which is inside the
asweigart folder, which is inside the Users folder.
Figure 8-1 shows this folder organization.

➢ The C:\ part of the path is the root folder, which contains all other folders.
➢ On Windows, the root folder is named C:\ and is also called the C: drive.
➢ On OS X and Linux, the root folder is /.

27
➢ Additional volumes, such as a DVD drive or USB thumb drive, will appear
differently on different operating systems.
➢ On Windows, they appear as new, lettered root drives, such as D:\ or E:\.
➢ On OS X, they appear as new folders under the /Volumes folder.
➢ On Linux, they appear as new folders under the /mnt (“mount”) folder.
➢ Also note that while folder names and filenames are not case sensitive on
Windows and OS X, they are case sensitive on Linux.

Backslash on Windows and Forward Slash on OS X and Linux

➢ On Windows, paths are written using backslashes (\) as the separator between
folder names.
➢ OS X and Linux, however, use the forward slash (/) as their path separator.
➢ If you want your programs to work on all operating systems, you will have to
write your Python scripts to handle both cases.
➢ This is simple to do with the os.path.join() function.
➢ If you pass it the string values of individual file and folder names in your path,
os.path.join() will return a string with a file path using the correct path
separators.
Enter the following into the interactive shell:

➢ In Windows os.path .join('usr', 'bin', 'spam') will return 'usr\\bin\\spam'.


(Notice that the backslashes are doubled because each backslash needs to be
escaped by another backslash character.)
➢ In OS X or Linux, the string would have been 'usr/bin/spam'.

➢ The os.path.join() function is helpful if you need to create strings for filenames.
➢ These strings will be passed to several of the file-related functions.

28
For example, the following example joins names from a list of filenames to the end of a
folder’s name:

Ex:
>>> myFiles = ['accounts.txt', 'details.csv', 'invite.docx']
>>> for filename in myFiles:
print(os.path.join('C:\\Users\\asweigart', filename))

Output:
C:\Users\asweigart\accounts.txt
C:\Users\asweigart\details.csv
C:\Users\asweigart\invite.docx

The Current Working Directory


➢ Every program that runs on the computer has a current working directory, or
cwd.
➢ Any filenames or paths that do not begin with the root folder are assumed to be
under the current working directory.
➢ You can get the current working directory as a string value with the os.getcwd()
function and change it with os.chdir().

Enter the following into the interactive shell:

➢ The current working directory is set to C:\Python34,


➢ The filename project.docx refers to C:\Python34\project.docx.

29
➢ When we change the current working directory to C:\Windows, project.docx is
interpreted as C:\Windows\project.docx.
➢ Python will display an error if you try to change to a directory that does not
exist.

Absolute vs. Relative Paths


➢ There are two ways to specify a file path.
➢ An absolute path, which always begins with the root folder.
➢ A relative path, which is relative to the program’s current working directory.
➢ There are also the dot (.) and dot-dot (..) folders.
➢ These are not real folders but special names that can be used in a path.
➢ A single period (“dot”) for a folder name is shorthand for “this directory.”
➢ Two periods (“dot-dot”) means “the parent folder.”

Figure 8-2 is an example of some folders and files.

When the current working directory is set to C:\bacon, the relative paths for
the other folders and files are set as they are in the figure.

30
Creating New Folders with os.makedirs()

A program can create new folders (directories) with the os.makedirs() function.

Enter the following into the interactive shell:

➢ This will create not just the C:\delicious folder but also a walnut
folder
➢ inside C:\delicious and a waffles folder inside C:\delicious\walnut.
➢ That is, os.makedirs() will create any necessary intermediate folders in
order to ensure that the full path exists.

31
Figure 8-3 shows this hierarchy of folders.

The os.path Module


➢ The os.path module contains many helpful functions related to
filenames and file paths.
➢ For instance, os.path.join() will build paths in a way that will work on
any operating system.
➢ Since os.path is a module inside the os module, you can import it by
simply running import os.

32
Handling Absolute and Relative Paths
The os.path module provides functions for returning the absolute path of a
relative path and for checking whether a given path is an absolute path.

➢ Calling os.path.abspath(path) will return a string of the absolute path


of the argument.
➢ This is an easy way to convert a relative path into an absolute one.
➢ Calling os.path.isabs(path) will return True if the argument is an
absolute path and False if it is a relative path.
➢ Calling os.path.relpath(path, start) will return a string of a relative
path from the start path to path.
➢ If start is not provided, the current working directory is used as the
start path.

Try these functions in the interactive shell:

Since C:\Python34 was the working directory when os.path.abspath() was called, the
“single-dot” folder represents the absolute path 'C:\\Python34'.
Enter the following calls to os.path.relpath() into the interactive shell:

33
Calling os.path.dirname(path) will return a string of everything that comes before the
last slash in the path argument.
Calling os.path.basename(path) will return a string of everything that comes after the
last slash in the path argument.

The dir name and base name of a path are outlined in Figure 8-4.

34
Finding File Sizes and Folder Contents
Once you have ways of handling file paths, you can then start gathering
information about specific files and folders.
The os.path module provides functions for finding the size of a file in bytes and the
files and folders inside a given folder.
Calling os.path.getsize(path) will return the size in bytes of the file in the path
argument.
Calling os.listdir(path) will return a list of filename strings for each file in the path
argument. (Note that this function is in the os module, not os.path.)

35
Checking Path Validity

Many Python functions will crash with an error if you supply them with a path that
does not exist.
The os.path module provides functions to check whether a given path exists and
whether it is a file or folder.
Calling os.path.exists(path) will return True if the file or folder referred to in the
argument exists and will return False if it does not exist.
Calling os.path.isfile(path) will return True if the path argument exists and is a file and
will return False otherwise.
Calling os.path.isdir(path) will return True if the path argument exists and is a folder
and will return False otherwise.

36
The File Reading/Writing Process

➢ Once you are comfortable working with folders and relative paths, you’ll be able
to specify the location of files to read and write.
➢ Plaintext files contain only basic text characters and do not include font, size, or
color information.
➢ Text files with the .txt extension or Python script files with the .py extension are
examples of plaintext files.
➢ These can be opened with Windows’s Notepad or OS X’s TextEdit application.
➢ Programs can easily read the contents of plaintext files and treat them as an
ordinary string value.
➢ Binary files are all other file types, such as word processing documents, PDFs,
images, spreadsheets, and executable programs.
➢ If you open a binary file in Notepad or TextEdit, it will look like scrambled
nonsense, like in Figure 8-5.

➢ Since every different type of binary file must be handled in its own way, this
book will not go into reading and writing raw binary files directly.
➢ Fortunately, many modules make working with binary files easier

37
There are three steps to reading or writing files in Python.
1. Call the open() function to return a File object.
2. Call the read() or write() method on the File object.
3. Close the file by calling the close() method on the File object

Opening Files with the open() Function


➢ To open a file with the open() function, you pass it a string path indicating the
file you want to open; it can be either an absolute or relative path.
➢ The open() function returns a File object.
➢ Try it by creating a text file named hello.txt using Notepad or TextEdit.
➢ Type Hello world! as the content of this text file and save it in your user home
folder.
Enter the following into the interactive shell:

>>> helloFile = open('C:\\Users\\Newfolder\\hello.txt')

➢ This command will open the file in read mode.


➢ When a file is opened in read mode, Python lets you only read data from the file;
you can’t write or modify it in any way.
➢ Read mode is the default mode for files you open in Python.
➢ But if you don’t want to rely on Python’s defaults, you can explicitly specify the
mode by passing the string value 'r' as a second argument to open().
➢ So open('/Users/ Newfolder /hello.txt', 'r') and open('/Users/ Newfolder
/hello.txt') do the same thing.
➢ The call to open() returns a File object.
➢ A File object represents a file on your computer; it is simply another type of
value in Python, much like the lists and dictionaries you’re already familiar with.
➢ In the previous example, you stored the File object in the variable helloFile.

38
➢ Now, whenever you want to read from or write to the file, you can do so by
calling methods on the File object in helloFile.

Reading the Contents of Files


read() : The read() method returns the specified number of bytes from the file.
Default is -1 which means the whole file.

Syntax:
file.read(size)

size Optional. The number of bytes to return. Default -1, which means the whole file.

Example:
f = open("demofile.txt", "r")
print(f.read(33))
Output:
Hello! Welcome to demofile.txt

readline() : The readline() method returns one line from the file.
It can also specify how many bytes from the line to return, by using the size parameter
Syntax:
file.readline(size)

size Optional. The number of bytes from the line to return. Default -1, which
means the whole line

39
Example:
f = open("demofile.txt", "r")
Demofile.txt
print(f.readline())
Hello! Welcome to
demofile.txt
Output:
This file is for testing
Hello! Welcome to demofile.txt
purposes.
'Good Luck!'
f = open("demofile.txt", "r")
print(f.readline(5))
print(f.readline())
Output:
Hello
This file is for testing purposes.

readlines() : The readlines() method returns a list containing each line in the file as a
list item.
Use the hint parameter to limit the number of lines returned.
If the total number of bytes returned exceeds the specified number, no more lines are
returned.
Syntax:

file.readlines(hint)

hint Optional. If the number of bytes returned exceed the hint number, no more lines will be
Default value is -1, which means all lines will be returned.

Example:
Return all lines in the file, as a list where each line is an item in the list object:

40
f = open("demofile.txt", "r")
print(f.readlines())
output:
['Hello! Welcome to demofile.txt\n', 'This file is for testing purposes.\n',
'Good Luck!']

Example
Do not return the next line if the total number of returned bytes are more
than 33:
f = open("demofile.txt", "r")
print(f.readlines(33))

Output:
['Hello! Welcome to demofile.txt\n', 'This file is for testing purposes.\n']

Note: ‘\n’ is treated as a special character of two bytes.

41
Note that each of the string values ends with a newline character, \n , except for the
last line of the file. A list of strings is often easier to work with than a single large
string value.

Writing to Files
42
The write() method writes a specified text to the file.
Where the specified text will be inserted depends on the file mode and stream
position.
"a": The text will be inserted at the current file stream position, default at the end of
the file.
"w": The file will be emptied before the text will be inserted at the current file stream
position, default 0.

Syntax:
file.write(text)

byte The text or byte object that will be inserted.

Example:
f = open("demofile2.txt", "a")
f.write("See you soon!") Demofile2.txt
f.close() Hello! Welcome to
demofile.txt
#open and read the file after the This file is for testing
appending: purposes.
f = open("demofile2.txt", "r") 'Good Luck!'
print(f.read())

Output:
Hello! Welcome to demofile2.txt
This file is for testing purposes.
Good Luck!See you soon!

43
Note that the write() method does not automatically add a newline character to the
end of the string like the print() function does. You will have to add this character
yourself.
44
Saving Variables with the shelve Module

➢ You can save variables in your Python programs to binary shelf files using the
shelve module.
➢ This way, your program can restore data to variables from the hard drive.
➢ `The shelve module will let you add Save and Open features to your program.
➢ For example, if you ran a program and entered some configuration settings, you
could save those settings to a shelf file and then have the program load them
the next time it is run.

Enter the following into the interactive shell:

➢ To read and write data using the shelve module, you first import shelve.
➢ Call shelve.open() and pass it a filename, and then store the returned shelf value
in a variable.
➢ You can make changes to the shelf value as if it were a dictionary.
➢ When you’re done, call close() on the shelf value.
➢ Here, our shelf value is stored in shelfFile.
➢ We create a list cats and write shelfFile['cats'] = cats to store the list in shelfFile
as a value associated with the key 'cats'(like in a dictionary).
➢ Then we call close() on shelfFile.
45
➢ After running the previous code on Windows, you will see three new files in the
current working directory: mydata.bak, mydata.dat, and mydata.dir.
➢ On OS X, only a single mydata.db file will be created.
➢ These binary files contain the data you stored in your shelf.
➢ The module frees you from worrying about how to store your program’s data to
a file.
➢ These programs can use the shelve module to later reopen and retrieve the data
from these shelf files.
➢ Shelf values don’t have to be opened in read or write mode—they can do both
once opened.

Enter the following into the interactive shell:

➢ Here, we open the shelf files to check that our data was stored correctly.
➢ Entering shelfFile['cats'] returns the same list that we stored earlier, so we know
that the list is correctly stored, and we call close().

➢ Just like dictionaries, shelf values have keys() and values() methods that will
return list-like values of the keys and values in the shelf.
➢ Since these methods return list-like values instead of true lists, you should pass
them to the list() function to get them in list form.

Enter the following into the interactive shell:

46
Saving Variables with the pprint.pformat() Function

➢ The pprint.pformat() function will return the content of a list or a dictionary


same text as a string instead of printing it.
➢ Not only is this string formatted to be easy to read, but it is also syntactically
correct Python code.
➢ Say you have a dictionary stored in a variable and you want to save this variable
and its contents for future use.
➢ Using pprint.pformat() will give you a string that you can write to .py file.
➢ This file will be your very own module that you can import whenever you want
to use the variable stored in it.

For example, enter the following into the interactive shell

47
➢ Here, we import pprint to let us use pprint.pformat().
➢ We have a list of dictionaries, stored in a variable cats.
➢ To keep the list in cats available even after we close the shell, we use
pprint.pformat() to return it as a string.
➢ Once we have the data in cats as a string, it’s easy to write the string to a file,
which we’ll call myCats.py.
➢ The modules that an import statement imports are themselves just Python
scripts.
➢ When the string from pprint.pformat() is saved to a .py file, the file is a module
that can be imported just like any other.
➢ And since Python scripts are themselves just text files with the .py file extension,
➢ Python programs can even generate other Python programs.
➢ You can then import these files into scripts

48

You might also like