Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

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

Rating
-
Sold
-
Pages
18
Grade
A
Uploaded on
16-08-2022
Written in
2022/2023

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- University of the People

Show more Read less
Institution
Course

Content preview

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

Written for

Course

Document information

Uploaded on
August 16, 2022
Number of pages
18
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$16.48
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Millenialnurse Freelance Tutor
Follow You need to be logged in order to follow users or courses
Sold
251
Member since
4 year
Number of followers
147
Documents
1482
Last sold
1 week ago
Genuine Study Guides 2024

Holla me for assistance 24/7.

3.4

37 reviews

5
17
4
4
3
4
2
2
1
10

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions