Module 3 Python (Chap 2)
Module 3 Python (Chap 2)
Module 3 Python (Chap 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.
On Windows, the root folder is named C:\ and is also called the C: drive.
On OS X and Linux, the root folder is /.
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:
Here, the current working directory is set to C:\Python34, so the filename project.docx refers to
C:\Python34\project.docx.
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:
For instance, we have already used os.path.join() to build paths in a way that will work on any operating
system.
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:
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.
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.
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.
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.
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.
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.
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.
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.
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.