Module 3 Python (Chap 2)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

MODULE 3
CHAPTER 2: 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 the 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 the 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.


 In the below example: 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 /.

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.
 We need to write Python scripts to handle both cases.
 This task is simple to do with the os.path.join() function.
Department of AI&ML, CIT- Ponnampet Page 1
Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

 We need to pass the string values of individual file and folder names in the path, os.path.join() will
return a string with a file path using the correct path separators.

 The os.path.join() function is helpful if you need to create strings for filenames.

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

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.
 We can get the current working directory as a string value with the os.getcwd() function
and change it with os.chdir().
 The following is the snippet code illustrating the os.getcwd() function and os.chdir() function.


 Here, the current working directory is set to C:\Python34, so the filename project.docx refers to
C:\Python34\project.docx. 

Department of AI&ML, CIT- Ponnampet Page 2


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

 When we change the current working directory to C:\Windows, project.docx is interpreted as


C:\ Windows\project.docx.

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.
 Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate
into the current working directory and the parent directory.
 Double dots are used for moving up in the hierarchy.
 A single dot represents the current directory itself.
 The following figure 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.

Creating New Folders with os.makedirs()


 Programs 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 as shown below:

Department of AI&ML, CIT- Ponnampet Page 3


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

The os.path Module


 The os.path module contains many helpful functions related to filenames and file paths.

 For instance, we have already used os.path.join() to build paths in a way that will work on any operating
system.

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. 

Department of AI&ML, CIT- Ponnampet Page 4


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)


 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 the below figure. 


 Example:

 If we need a path’s dir name and base name together, we can just call os.path.split() to get a tuple
value with these two strings like shown below:

Department of AI&ML, CIT- Ponnampet Page 5


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

 Notice that you could create the same tuple by calling os.path.dirname() and os.path.basename()
and placing their return values in a tuple.

 But os.path.split() is a nice shortcut if you need both values.

 Note that os.path.split() does not take a file path and return a list of strings of each folder.

 For that, use the split() string method and split on the string in os.sep.

Finding File Sizes and Folder Contents

 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.) 


 The calc.exe program on my computer is 776,192 bytes in size, and there are lot of files in
C:\Windows\system32.

 If we want to find the total size of all the files in this directory, then we can use os.path.getsize()
and os.listdir() together.

Department of AI&ML, CIT- Ponnampet Page 6


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)


Checking Path Validity

 Many Python functions will crash with an error if we 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. 

Department of AI&ML, CIT- Ponnampet Page 7


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

The File Reading/Writing Process


 Two types of files
 Plain Text Files are the ones that contain only basic text characters and do not
include font, size, or color information. Example: Files with .txt extension and .py files
 Binary Files must be handled in its own way has it contains lot of information,
Example: Word processing documents, PDFs, images, spreadsheets, and executable
programs.
 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.
 Then, if you’re using Windows, enter the following into the interactive shell:

 Make sure to replace your_home_folder with your computer username.


 For example, if username of computer is asweigart, we should enter 'C:\\Users\\asweigart\\
hello.txt' on Windows.
 Both these commands will open the file in “reading plaintext” mode, or read mode for short.
 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/asweigart/ hello.txt', 'r') and open('/Users/asweigart/hello.txt') do the same thing.

Department of AI&ML, CIT- Ponnampet Page 8


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

 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.
Reading the contents of files
 To read the entire contents of a file as a string value, use the File object’s read() method.


 We can use the readlines() method to get a list of string values from the file, one string for each
line of text.
 For example, create a file named sonnet29.txt and write the following text in it: 


 Make sure to separate the four lines with line breaks. Then enter the following into the interactive
shell:


 Note that each of the string values ends with a newline character, \n , except for the last line of
the file.

Writing to Files
 To write content to a file we can use write() function “writes”.
 We can’t write to a file which is opened in read mode. We need to open it in “write plaintext”
mode or “append plaintext” mode, or write mode and append mode for short.
 Write mode will overwrite the existing file and start from the beginning , Pass 'w' as the second
argument to open() to open the file in write mode.

Department of AI&ML, CIT- Ponnampet Page 9


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

 Append mode, on the other hand, will append text to the end of the existing file. Pass 'a' as
the second argument to open() to open the file in append mode.
 If the filename passed to open() does not exist, both write and append mode will create a
new,
blank file.
 After reading or writing a file, call the close() method before opening the file again.


 In the above code snippet, initially we open bacon.txt in write mode. 
 Since there isn’t a bacon.txt yet, Python creates one. 
 Calling write() on the opened file and passing write() the string argument 'Hello world! /n' writes
the string to the file and returns the number of characters written, including the newline. 
 Then we close the file. 
 To add text to the existing contents of the file instead of replacing the string we just wrote, we
open the file in append mode. 
 We write 'Bacon is not a vegetable.' to the file and close it. 
 Finally, to print the file contents to the screen, we open the file in its default read mode, call
read(), store the resulting File object in content, close the file, and print content. 

Saving Variables with the shelve Module


 We can save variables in the Python programs to binary shelf files using the shelve module.
 This way, our program can store program’s data to a file.
 The programs can use the shelve module to later reopen and retrieve the data from these shelf
files.

Department of AI&ML, CIT- Ponnampet Page 10


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

 Shelf values don’t have to be opened in read or write mode—they can do both once opened.


 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. 
 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. 
 Your 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. 


 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. 
Department of AI&ML, CIT- Ponnampet Page 11
Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)


Saving Variables with the pprint.pformat() Function
 The pprint.pprint() function will “pretty print” the contents of a list or dictionary to the screen
 Similarly, we have pprint.pformat() function will return this same text as a string instead of
printing it.
 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.

 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.
 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.

Department of AI&ML, CIT- Ponnampet Page 12


Introduction to Python Programming (BPLCK105B / 205B) Module 03 (Chapter 2)

 The benefit of creating a .py file (as opposed to saving variables with the shelve module)

is that because it is a text file, the contents of the file can be read and modified by anyone
with a simple text editor.
 For most applications, however, saving data using the shelve module is the preferred way
to save variables to a file.

Department of AI&ML, CIT- Ponnampet Page 13

You might also like