This document explains how to create, read, and save files in Python, emphasizing the importance of file paths and the differences in path formatting across operating systems. It introduces the pathlib module for handling file paths and demonstrates how to use the / operator to join paths safely and consistently. The document also highlights the significance of using the correct path separators and the case sensitivity of filenames on different systems.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
0 views35 pages
Module 3(Chap2) python programming module
This document explains how to create, read, and save files in Python, emphasizing the importance of file paths and the differences in path formatting across operating systems. It introduces the pathlib module for handling file paths and demonstrates how to use the / operator to join paths safely and consistently. The document also highlights the significance of using the correct path separators and the case sensitivity of filenames on different systems.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 35
9
READING AND WRITING FILES
Variables are a fine way to store data while your program is running, but if you want
your data to persist even after your program has finished, you need to save it to a file.
You can think of a file’s contents as a single string value, potentially gigabytes in size. In
this chapter, you will leam how to use Python to create, read, and save files on the hard
drive.
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 laptop with the filename project.docx in the path C:\Users\Al\Documents. The
part of the filename after the last period is called the file’s extension and tells you a file’s
type. The filename project.docx is a Word document, and Users, Al, 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 Al folder, which is
inside the Users folder. Figure 9-1 shows this folder organization.CN
Le ses
L Al
Lips Decne
Li psc
Figure 9-1: A file in a hierarchy of folders
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 macOS and
Linux, the root folder is /, In this book, I’ll use the Windows-style root folder, If you
are entering the interactive shell examples on macOS or Linux, enter / instead.
Additional volumes, such as a DVD drive or USB flash drive, will appear differently
on different operating systems. On Windows, they appear as new, lettered root drives,
such as D:\ or E:|. On macOS, 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 macOS, they are
case-sensitive on Linux.
Since your system probably has different files and folders on it than mine, you won't
be able to follow every example in this chapter exactly. Still, try to follow along
using folders that exist on your computer.
Backslash on Windows and Forward Slash on macOS and Linux
On Windows, paths are written using backslashes (\) as the separator between folder
names. The macOS and Linux operating systems, 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.
Fortunately, this is simple to do with the path() function in the pathiib module. If you
it the string values of individual file and folder names in your path, path() will return
Pi
a string with a file path using the correct path separators. Enter the following into the
interactive shell:>>> from pathlib import Path
>>> Path('spam', "bacon", ‘eggs')
WindowsPath( "spam/bacon/eggs" )
>>> str(Path('spam', ‘bacon’, ‘eggs'))
* spam\ \bacon\\eges*
Note that the convention for importing pathiib is to run from p:
ib import Path, since
otherwise we'd have to enter pathiib.path everywhere path shows up in our code. Not only
is this extra typing redundant, but it’s also redundant.
I’m running this chapter’s interactive shell examples on Windows, so path(’ span’,
‘bacon’, ‘eggs') returned a windowspath object for the joined path, represented as
WindowsPath(span/bacon/eggs"). Even though Windows uses backslashes, the windowsPath
representation in the interactive shell displays them using forward slashes, since open
source software developers have historically favored the Linux operating system.
If you want to get a simple text string of this path, you can pass it to the str()
function, which in our example returns ‘span\\eacon\\eggs'. (Notice that the backslashes
are doubled because each backslash needs to be escaped by another backslash character.)
If I had called this function on, say, Linux, path() would have returned a posixpath object
that, when passed to str(), would have returned ‘span/bacon/eges'. (POSIX is a set of
standards for Unix-like operating systems such as Linux.)
These path objects (really, windousPath or Posixrath objects, depending on your operating
system) will be passed to several of the file-related functions introduced in this chapter.
For example, the following code joins names from a list of filenames to the end of a
folder’s name:
>>> from pathlib import Path
>>> myFiles = [‘accounts.txt’, ‘details.csv', ‘invite.docx’ ]
>>> for filenane in myFiles:
print(Path(r'C:\users\Al', filenane))
C:\users\Al\accounts.txt
C:\Users\al\details.csv
C:\Users\Al\invite.docx
On Windows, the backslash separates directories, so you can’t use it in filenames.
However, you can use backslashes in filenames on macO$ and Linux. So while
path(r'spam\eggs') refers to two separate folders (or a file eggs in a folder spam) on
Windows, the same command would refer to a single folder (or file) named spamleggson macOS and Linux. For this reason, it’s usually a good idea to always use forward
slashes in your Python code (and I’ll be doing so for the rest of this chapter). The pathiib
module will ensure that it always works on all operating systems.
Note that pathiib was introduced in Python 3.4 to replace older os.path funetions. The
Python Standard Library modules support it as of Python 3.6, but if you are working
with legacy Python 2 versions, I recommend using pathiib2, which gives you pathiib’s
features on Python 2.7. Appendix A has instructions for installing patniiba using pip.
Whenever I’ve replaced an older os.path function with pathiib, I've made a short note.
You can look up the older functions at https://docs.python.org/3/library/os,path.html.
Using the / Operator to Join Paths
‘We normally use the + operator to add two integer or floating-point numbers, such as in
the expression 2 + 2, which evaluates to the integer value 4. But we can also use the +
operator to concatenate two string values, like the expression ‘Hello’ + ‘world', which
evaluates to the string value ‘Hetloworid'. Similarly, the / operator that we normally use
for division can also combine path objects and strings. This is helpful for modifying a
path object after you’ve already created it with the path() function.
For example, enter the following into the interactive shell:
>>> from pathlib import path
>>> Path('spam') / ‘bacon’ / ‘eggs’
WindowsPath( 'span/bacon/eggs" )
>>> Path('spam') / Path(‘bacon/eggs")
WindowsPath( 'spam/bacon/eggs" )
>>> Path(‘spam') / Path(*bacon‘, ‘eggs")
WindowsPath ( "spam/bacon/eges' )
Using the / operator with rath objects makes joining paths just as easy as string
concatenation, It’s also safer than using string concatenation or the join() method, like
we do in this example:
>>> honeFolder = r'C:\Users\Al’
>>> subfolder = ‘span*
>>> honeFolder + '\\' + subFolder
“C:\\Users\\AI\\span*
>>> ‘\\' Join({homeFolder, subFolder])
*C:\\Users\\Al\\span”A script that uses this code isn’t safe, because its backslashes would only work on
Windows. You could add an i¢ statement that checks sys.platform (which contains a string
describing the computer’s operating system) to decide what kind of slash to use, but
applying this custom code everywhere it’s needed c
be inconsistent and bug-prone.
The patniib module solves these problems by reusing the math division operator to
join paths correctly, no matter what operating system your code is running on. The
following example uses this strategy to join the same paths as in the previous example:
>>> homeFolder = Path('C:/Users/Al')
>>> subFolder = Path('spam’)
>>> homeFolder / subFolder
WindowsPath("C:/Users/Al/spamn’ )
>>> str(homeFolder / subFolder)
*C:\\Users\\Al\\span”
The only thing you need to keep in mind when using the / operator for joining paths
is that one of the first two values must be a path object. Python will give you an error if
you try entering the following into the interactive shell:
>>> ‘spam’ / "bacon’ / ‘eggs’
Traceback (most recent call last):
File "", line 1, in
and ‘str
‘TypeError: unsupported operand type(s) for /: ‘str
Python evaluates the / operator from left to right and evaluates to a path object, so
cither the first or second leftmost value must be a path object for the entire expression to
evaluate to a path object. Here’s how the / operator and a path object evaluate to the final
path object.
Path("spam)/"bacon’
WindowsPath('span/bacon")/"eges'
WindowsPath("spam/bacon/eges") /‘ham"
WindowsPath("spam/bacon/eges/ham' )
Tf you see the typetrror: unsupported operand type(s) for /: ‘str’ and ‘str’ error message
shown previously, you need to put a path object on the left side of the expression