The Journey Continues…

Dan Eder
2 min readAug 18, 2019

--

Another day, more coding. I’ve resolved to code Python every single day to help stay fluent and learn as much as I can. Not sure if I’ll be able to update this blog as regularly, but stranger things have happened.

Today I focused on reading. One thing I have had to learn (often the hard way through wasted time) is that I love to dive into problems. I love to formulate new, fun ways to solve coding problems. This enthusiasm sometimes makes me overlook the reading and learning aspect of this work.

“It can’t all be doing. Some of it has to be absorbing.” — The World’s Worst Monk

After bumping into a wall on my Exercism.io assignment for the week, I dipped back into Automate the Boring Stuff tonight, specifically Chapter 8, which deals with writing and saving files from the command line.

I knew a bit about this subject but sorely needed a refresher and, as always, appreciated his clear way of explaining things. Some highlights:

Useful Commands

“.”- shorthand for ‘this directory’, useful in filepaths

“..”- shorthand for ‘back one up from this directory’, super useful for navigating

Absolute vs. Relative Paths

An Absolute Path starts from the root folder (‘/’ on Mac)

A Relative Path picks up somewhere after that, doesn’t describe full path from root

Open() Function

You can pass the Open() function a file path or a variable, depending on how it’s set up.

You can declare a variable for the results of the open function:

helloFile = open('/Users/your_home_folder/hello.txt')

A nifty way to tap into a file in a clean way.

Writing / Overwriting Files

You can also pass ‘w’ as the second argument to open:

baconFile = open('bacon.txt', 'w')

This overwrites the file. (Similarly, ‘a’ appends new info)

Validating File Paths

I could see this coming in handy with debugging things. Going to copy cut and paste the guide here, bc Sweigert is the man:

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

Not all of these worked for me in the IDLE shell, but I had to move on to keep going. Maybe I can revisit them before I actually need them….

Shelve2

This one took me some hunting to figure out. After a frustrating 15 mins of getting errors I did some googling. Turns out Shelve was modified to Shelve2, I had to look in the docs to find that out and install the newer version (I guess the older was deprecated).

Shelve is useful for sharing your data outside of your program as a dictionary in a ‘.db’ file.

>>> import shelve
>>> shelfFile = shelve.open('mydata')
>>> cats = ['Zophie', 'Pooka', 'Simon']
>>> shelfFile['cats'] = cats
>>> shelfFile.close()

I’ve stopped at the first project, Generating Random Quiz Files, hoping to pick up on it tomorrow…

--

--

Dan Eder
Dan Eder

Written by Dan Eder

Full Stack Web Developer, polyglot, songwriter, always learning

No responses yet