100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
COMP CS 1101 Discussion Unit 8 CS 1101- University of the People $15.98   Add to cart

Exam (elaborations)

COMP CS 1101 Discussion Unit 8 CS 1101- University of the People

 1 view  0 purchase
  • Course
  • Institution

COMP CS 1101 Discussion Unit 8 CS 1101- University of the People/COMP CS 1101 Discussion Unit 8 CS 1101- University of the People/COMP CS 1101 Discussion Unit 8 CS 1101- University of the People/COMP CS 1101 Discussion Unit 8 CS 1101- University of the People/COMP CS 1101 Discussion Unit 8 CS 1101-...

[Show more]

Preview 3 out of 18  pages

  • August 16, 2022
  • 18
  • 2022/2023
  • Exam (elaborations)
  • Questions & answers
avatar-seller
Describe how catching exceptions can help with file errors. Write three Python
examples that actually generate file errors on your computer and catch the errors
with try: except: blocks. Include the code and output for each example in your post.

Describe how you might deal with each error if you were writing a large production
program. These descriptions should be general ideas in English, not actual Python
code.



68 words


Permalink | Reply



Re: Discussion Forum Unit 8
by Matthew Gayton - Thursday, 24 October 2019, 11:01 PM

import os

# As described in our text, a file access attempt may result in a number
# of different errors. These are program stopping errors unless the program
# has a method of 'catching' the error and redirecting what action should
# take place should that error occur. One would prefer to have their program
# not crash should a file be moved, missing, protected, or what-have-you.

# for example, the file one is trying to open may no longer be there.
# This call tries to open a mystery_file in the current working directory:
# fin = open("antenna_module.txt")

# this results in the error:
# fin = open("mystery_file")
# FileNotFoundError: [Errno 2] No such file or directory: 'mystery_file'

# to avoid this we can 'try' to open the file and see what happens,
# then take a course of action based on the result of that try:
try:
fin = open("antenna_module.txt")
except:
print("I can't do that Dave.")

,# that's what happens when we try to read a file that isn't there, but
# what happens if we try to write to a file that is 'read-only'? I created
# a file called 'locked_pod.txt' and placed it in my current working
# directory (Downey, A, 2015):

try:
fout = open("locked_pod.txt", "w")
except:
print("What are you doing, Dave?")


cwd = os.getcwd()
print("\nOur current working directory is: ", cwd)
print("and trying to write to a directory, instead of a file: ")

try:
fin = open("/D:University")
except:
print("I can't allow you to do that Dave.")

# While these examples give a rudimentary idea of how to program
# so that errors get some attention, in a large production program
# some of these errors would not occur (for example when opening a
# file in a graphical operating system there would be a 'file open'
# dialog box that would only show the type of files allowed) and
# the errors that could happen would be given more elegant solutions
# that would range from something as simple as triggering the system
# alert sound to giving the user a clear and direct explanation
# of what went wrong and how to correct it (if possible). All programs
# can crash and if written and bug-tested thoroughly will do so very
# infrequently. By creating functions that handle as many exceptions
# as possible within the program we will be going a long way toward
# producing professional level programs.

# Reference
# Downey, A. (2015) Think Python, How to think like a computer scientist.
# This book licensed under Creative Commons Atribution-Non-Commercial
# 3.0 Unported (CC BY-NC 3.0)

, Output:
PS D:\University\Term 1\Introduction to Computer Programming\Homework\Unit
8> & "C:/Program Files (x86)/Microsoft Visual
Studio/Shared/Python37_64/python.exe" "d:/University/Term 1/Introduction to
Computer Programming/Homework/Unit 8/Discussion_Forum_Unit_8.py" I can't do
that Dave. What are you doing, Dave? Our current working directory is:
D:\University\Term 1\Introduction to Computer Programming\Homework\Unit 8
and trying to write to a directory, instead of a file: I can't allow you to do that Dave.
PS D:\University\Term 1\Introduction to Computer Programming\Homework\Unit
8>



516 words


Permalink | Show parent | Reply



Re: Discussion Forum Unit 8
by Dain Anderson - Monday, 28 October 2019, 3:08 PM

Hi Matthew,

Nice 2001 references, I got a kick out of your examples. I wonder if you're in the
same boat as me on this one, where every single time I go to type 'except:', I instead
type 'catch:' -- old habits are hard to break! One thing I looked up that's quite useful
with try/except blocks is that they also support the 'finally:' construct, which is
superb for closing out logic, logging, or other routines that must happen, regardless
of whether or not the code succeeded or failed.

Good luck on your finals and I'm sure I'll see you around!



101 words


Permalink | Show parent | Reply



Re: Discussion Forum Unit 8

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller Millenialnurse. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $15.98. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

67163 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$15.98
  • (0)
  Add to cart