Automate the Boring Stuff — Chapter 8

Dan Eder
1 min readOct 9, 2019

--

Reading & Writing Files

Working my way through Chapter 8 of Al Sweigart’s Automate the Boring Stuff With Python. As I’m just trying to do more observation in general, thought this was a good enough place to jot down a few things to help burn them into my memory.

First, a file path is just a string. For instance:

helloFile = open(‘/Users/al_sweigart/Desktop/hello.txt’)

So often what we’re doing in this chapter is saving these complex file path strings to variables which can then be manipulated.

Some definitions:

Absolute path- includes the root

Relative path- no root, relative to cwd

3 “mode” arguments that can be passed to the open() function: read (‘r’), write (‘w’), append (‘a’). Write will overwrite previous data. Append just adds to the end.

read() method presents data as a string, readlines() presents it as a list, one string for each line of text.

The Shelf module can be used to store program data on the hard drive for use later. Similar to a dictionary, with key and value pairs.

The .. directory is the directory above the cwd (parent directory), The . directory is the cwd.

os.chdir(directory) = to jump to a new directory

os.getcwd = to find out cwdr

More to come tomorrow when I work on the Multiclipboard…

--

--

Dan Eder
Dan Eder

Written by Dan Eder

Full Stack Web Developer, polyglot, songwriter, always learning

No responses yet