CS 1101 - AY2022-T2 - Discussion Unit 8
CS 1101 - AY2022-T2 - Discussion Unit 8
Search forums
The cut-off date for posting to this forum is reached so you can no longer post to it.
Discussion Unit 8
by Leonidas Papoulakis (Instructor) - Wednesday, 10 November 2021, 7:12 AM
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
Catching exceptions that uses the try: statements can be used to resolve file errors. This is achieved by checking each part of
our file when our code is been ran. When the part of the file that is been run is error free, the code will keep on running. But if
an error is found, we can use the second part of the try: statement to identify the error and fix it in other to stop the program
from crashing
2. 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.
Example 1
Because we can’t add the string int; the try statement will go directly to except
İnput:
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 1/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
try:
print('Hello'+987)
except:
print(''error 1'')
Output
== RESTART: C:/Users/user/AppData/Local/Programs/Python/Python39/test code.py ==
error 1
Example 2
When we input a number, the code will run smoothly while, if we change the number to a character, it will immediately go to
except.
İnput:
try:
v = int(input("enter a number: "))
except:
print("error 2")
Output:
== RESTART: C:/Users/user/AppData/Local/Programs/Python/Python39/test code.py==
enter a number: 1
>>>
== RESTART: C:/Users/user/AppData/Local/Programs/Python/Python39/test code.py==
enter a number: @
error 2
Example 3
By not declaring (j), an error will be presented, while the error will be printed by the try statement.
İnput
try:
print(j)
except NameError:
print(''Error 3'')
Output
== RESTART: C:/Users/user/AppData/Local/Programs/Python/Python39/test code.py
==
Error 3
3. 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 an actual Python code.
If I am faced with a large production program, I will look deep into each section and reduce the amount of code inside
thestatement until I locate the exact part of the code that is not working. It is also important to include specific exceptions that
will help recovering and handling errors that might arise when using the program, such as: except TypeError and ValueError.
343 words
The brief appears to ask for examples of "file errors" rather than standard python errors as you have demonstrated. Based
on the reading material for this week it would appear to be asking for file read or write errors and how can we avoid them.
Nonetheless you have demonstrated an understanding of the try: except: structure
56 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 2/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Dear Ahmet, it a nice example you provided regarding exception handling. But it would be better if you had understood
the assignment and answered based on that. Because we were supposed to answer for file-related exception catching.
37 words
Hello ahmet, you did a great work in explaining how exception handling helps but the examples must be on files.
20 words
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:
Answer
"A lot of things can go wrong when you try to read and write files. If you try to open a file
that doesn’t exist, you get an IOError" (Downey, 2015). This shows that there is a bad result if we do not handle the exception
before it is caused. This is because we write a program to solve problems and the end-user may not know how it is doing
things. So, he can not understand what it is saying. Therefore, we have to catch the exception and write something the user
can understand. This is called exception handling. This can be done using try: ............ except/ finally block. So, examples are
below.
File
"F:\Programing
I\CS1010\Chapter4\Written_Assignment\os_manupilation.py", line 13, in
<module>
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 3/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
search_dir('D:')
File
"F:\Programing
I\CS1010\Chapter4\Written_Assignment\os_manupilation.py", line 5, in
search_dir
for name in
os.listdir(dir_name):
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 4/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
C:\Users\PC\AppData\Local\Programs\Python\Python310\python.exe
"F:/Programing
I/CS1010/Chapter4/Written_Assignment/os_manupilation.py"
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 5/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
From the above output, we can see that the interpreter throws
a file not found an exception. So, let us handle this error using
try: except. The script
is as follows.
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 6/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist.
637 words
Tags:
Discussion Forum Unit 8
Excellent post with a diverse group of file errors to demonstrate your understanding of this weeks subject material. It is
clear you are very familiar with python and your level seems to be more advanced than this course. You always do a great
job of explaining and provide good examples. great job
52 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 7/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Hello Samrawi, as always you did a great job in explaining and giving the right examples clearly. Well done!
19 words
Your explanation and input is very clear. It makes our work to understand the topic even easier. You have clearly explained.
Thank you for your input.
26 words
Excellent post as always. Easy to understand and easy to read. Great work.
13 words
Great one from you. Following your post will help others that doesn't understand the assignment have a better grasp
of it.
Keep it up
24 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 8/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Dear Samrawi,
Kind regards
15 words
First we have IOError which throws an error if the file we want to read doesn't exist.
Example:
code:
try:
f = open('fouad.txt','r')
except IOError:
output:
Here in the above code I tried to read a file that doesn't exist which will throws an error since the 'r' format doesn't create a file
it only read from the existing files.
Second we have PermissionError which throws an error if we want to read or write to a file we don't have the permission to
access.
Example:
code:
try:
f = open('fouad','w')
except PermissionError:
output:
In the above code I tried to write to a file that I don't have access to so the try catch block detect it with PermissionError.
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 9/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Third Error which is the IsADirectoryError, that throws an error is we are trying to open a directory to read from.
Example:
code:
import os
cwd = os.getcwd()
try:
f = open(cwd)
except IsADirectoryError:
print("This is a directory")
output:
This is a directory
Since os.getcwd() returns the current directory the try except block throws an error since we cannot open a directory to read
from.
In building large programs while writing a code to open a file I would put each time I open a file in a try except block and
writing the suitable printing method to make it more obvious to what error I am dealing with so I can fix it.
256 words
Concise coverage of 3 different file errors and how to avoid them. The post is a little short and you could have gone into
more depth with your answer to the last question.
33 words
nice work
2 words
Dear Fouad, fantastic explanation. You have made a great job with clear and precise examples. I have learned something
different which I did not know before that is "IsADirectoryError" to check whether it is a directory or not. Thank you!
40 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 10/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Good work on your code. However, next time fix the spacing. The spacing disappears sometimes when you copy+paste, it
makes it hard to read if you don't fix it here. It would be nice to have a bit more on the explanations.
42 words
Hello Fouad, Thanks for the excellent effort put forth to clarity of the work.
14 words
great work
2 words
the try: except: structure in python is a great way to have your program still run even if there is an issue such as a file not
found, permission issues or the file is misspelled. You may want to have your code run on someone else's computer and their
directory structure or files are likely not the same as yours. Even if the program cannot run as intended this structure allows us
to raise an error which can give the user some idea of what is wrong.
Here are 3 example programs which demonstrate the usefulness of this structure.
if the required file does not exist we can notify the user so they can put that file in the correct directory.
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 11/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
In my second example I intentionally remove write permissions for the file I wish to access, to demonstrate the fact that if my
program is running on someone else's computer there is the potential that the file the program needs to access may for some
reason or other be set to read only permission.
I can get around this issue and notify the user of the problem with a well thought out error message.
Lastly there may be an issue that the volume that the user is trying to run the program on is a read only file system.
Again we can get past this with try: except: and inform the user of the issue so they can try running the program on a file
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 12/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
There are many things to consider when designing software that others will use. Portability is one key factor. We want to make
sure our code will run on as many platforms as possible without error. Designing a program which checks which OS the user is
running and altering the flow of the program based on that can help a great deal. If the program is designed to open and edit
.txt files and it doesn't matter what the file is called, we can use os.listdir() function along with string methods to check whether
there is .txt in the file name.
361 words
great work
2 words
Dear Luke, it is a great job, wow. Your explanation and the example you provided are on point. I am able to learn another
way of handling exceptions. You also well-understood what a software designing should be.
37 words
Hello Luke, You did a great job in your explanation and your discussion covers every detail that must be discussed. Thank
you and keep it up!
26 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 13/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
You have done a good job in solving this problem. You have clearly made a positive input in this discussion. Well done and
keep it up.
26 words
Hi Luke, Your work is well detailed and it shows your in-depth understand. Thanks for the contribution to make me learn
better with your input
25 words
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.
Example 1:
Input:
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 14/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Output:
Input:
Output:
Debug: exception happen= write() argument must be str, not int
Example 3:
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 15/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Input:
Output:
References:
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. This book is licensed under Creative
Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0).
146 words
Your use of function to except errors is clear that is working. You have also explained clearly how to solve the problems.
Your input has been positive . Keep it up.
31 words
Good work on your code. However, you left out one part of the prompt. You forgot to answer "Describe how you might
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 16/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
deal with each error if you were writing a large production program. These descriptions should be general ideas in English,
not actual Python code." Also it would be nice if you added more sentences into your discussion post. As well as
comments in your code.
67 words
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.
Example 1
try:
a = 10/1
print(a)
except:
OneDivisionError
print("One!")
Interpreter output
One!
Example 2
try:
Nor = 28
TypeError
print('Wrong type')
Interpreter output
Wrong type
Example 3
try:
nor = []
print(nor[1])
except:
IndexError
print("Index error")
Interpreter output
: D:\Users\Crystal Noralez\Documents\sync\UoPeople\Term 3\Programming Fundamentals\Unit 8\Assessments\Discussion
Forum Unit 8.py
Index error
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 17/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Reference
Downey, A. (2015). Think Python| How to Think Like a Computer Scientist: Vol. Version 2.2.23 (2nd Edition). Green Tea Press.
https://my.uopeople.edu/pluginfile.php/1404469/mod_page/content/3/TEXT%20-%20Think%20Python%202e.pdf
170 words
Nice examples
2 words
Thanks.
1 words
Catching exception helps in file errors. Ordinarily in python if you generate an error the program always gives an error
message that suggest what type of an error has been generated. One can deal with such errors by creating exception options .
Compiler errors such as syntax errors causes the program to return an error message. Such error messages are accompanied
by definition of the type of error one made or the system comes across with. Run time errors may be generated by the
program user and so one has to create exceptions for such errors in the process of generating the system. Below are 3
examples of the errors and their solutions or exception options.
https://my.uopeople.edu/pluginfile.php/1521295/mod_forum/post/15459474/discussion%20script.py
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 18/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
The easiest way to deal with the errors is by using except option in the program. This would really assist the users of the
program especially large programs to be able to identify the cause of the errors generated by the programs. This would further
guide the user on actions to take in order to have the correct feedback.
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 19/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
205 words
Dear Simon,
Great examples on how to use try and except statements, therefore the topic given was to explain the statements when we
deal with files. open(), read() etc...
Keep it up
Regards
33 words
Hi Simon,
each time your subsumptions get better , keep up the great work !
13 words
Catching exceptions can help with file errors since we can program
our code to tell us what exactly the error is if we know what
to do
to cause an error.
Input:
text = open('hello.txt','w')
text.write('Hello\n')
text.write('Good day\n')
text.close()
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 20/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Output:
Input 1:
def print_file(s):
try:
word = line.strip()
print(word)
except:
Output 1:
Hello
Good day
I can deal with this error by editing the code so if the file is not
found, it will create a new text file instead of just looking for a
nonexistent file.
Input 2:
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 21/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
import os
def new_dir(s):
try:
except:
new_file('folder') #call out function with a file that does not exist
Output 2:
I can deal with this error by changing it so that it will open the
directory instead if it already exists instead of making a new
one.
Input 3:
def write_in(s,t):
try:
ftxt.write(t)
ftxt.close()
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 22/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
print('Using a non-file:')
write_in('folder','textdoc')
print('Using a file:')
write_in('hello.txt','Hey')
Output 3:
Using a non-file:
Using a file:
335 words
Dear Janelle,
you showed a great understanding of the task given for this week. I like your examples as they helped me to understand
more about this week's topic.
Regards
30 words
Hi Janelle,
Exceptional work well done! You managed to solve the given problem in detail and accurately.
17 words
#The 3 python error-exception examples I want to give are the value error,
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 23/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
a=10
b=0
c=a/b
#try and except block controlled this error and give additional information about the message
try:
a = 10
b = 0
c = a / b
except ZeroDivisionError:
#value error
import math
num=-10
#This gave ValueError: math domain error. but using the try and except block, the
#error message was controlled and more information was given
try:
except ValueError:
#file not found error. Trying to open a file that does not exist.
contents=a.read
print(contents)
#running this file gave FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'
#But with the try and except block, I provided more information on the error
try:
contents=a.read
print(contents)
except FileNotFoundError:
print('File does not exist, please check that the file is in the present directory or correct path was given')
Output: File does not exist, please check that the file is in the present directory or correct path was given
Errors and bugs cannot be completely eliminated from any computer program, especially when working on a large production
program. In such, a good way to deal with errors is through good development and design. Incorporating incremental
development is a good way to reduce the chances of errors occurring in your program.
285 words
You did a vey excellent job on the explanation. Keep up the great job.
16 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 24/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Using a try statement can help us to check if the part of the code runs smoothly with no errors, in case the code has some
mistakes we could use except statement to catch the error and give the chance to fix it without crashing the program. It is very
similar that if and else.
Catching exceptions is a way to handle errors in a program that you don't know about at the time of coding the program.
The statements mentioned also can be very useful to help us when we work with files, we could add a block to try to manage
files and if the path and code are correct the program will run with no problems, otherwise, the exception will be called and
will catch the error.
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.
try:
# This will cause an error because the variables a and b are not defined
print(a + b)
except:
#Output:
# This example will open a file and print the contents of the file, if there is an error it will print the error,
but the program will not stop running and the file will be closed
try:
# Try to open the file in read mode but the path is not correct
except:
# Output:
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 25/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
# This example help us to write on files and print the contents of the file,
try:
# Try to open the file in write mode but the path is not correct
# write on the file, this part will make the program crash because the file was open ONLY in reading mode.
# Try to open the file in read mode but the path is not correct
f.close()
except:
446 words
2. 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.
3. 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.
QUESTION #1
Catching exceptions help us with file errors. It will try to run a part of our code so as to be able to check the other part of our
file. If the part we are testing does not have an error, the code will keep running. If an error is found, we can easily verify it with
the second part of the try statement “except”, which will help us identify the particular error. This is how we can fix the error
without the program ending up in a crash.
QUESTION #2
# Example 1: Error of adding a string to int. The try statement will go immediately to except.
+ Code:
try:
print("Jackson" + 429)
except:
print("error 1")
+ Output:
error 1
>>> # Example 2: Error of inputting a character when an int is originally declared. If we try to input a number, the code will run
normally, but if we put a character, it will go immediately to the except:
+ Code:
try:
except:
print("error 2")
+ Outputs:
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 26/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
enter a number: 24
error 2
>>> # Example 3: Error of not declaring a variable. The try statement will print the error:
+ Code:
try:
print(z)
except NameError
print("error 3")
+ Output:
error 3
>>>
QUESTION #3
In writing a large production program, I will reduce the number of code inside the try statement until I find the exact part of
the code that is not working. I will also include specific exception that will recover and handle such error that could happen
during the use of the program like except TypeError - ValueError.
Reference:
Python Exception Handling Using try, except and finally statement. (n.d). Programiz. https://www.programiz.com/python-
programming/exception -handling
380 words
catching exceptions can help with file errors by showing us what went wrong without stopping the program from
running.
Example
try:
path = 'myWebsite.com/offer'
with open(path) as f:
print(f)
except:
ry:
blue = 600
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 27/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
3. PermissionError
Raised when trying to run an operation without the adequate access rights
Example
try:
f = open("TopSecret.txt")
try:
f.write("data")
except:
finally:
f.close()
except:
119 words
All activities close on Wednesdays at 11:55 PM, except for Learning Journals/Portfolios which close on Thursdays at 11:55 PM always
following the clock at the top of the page.
Due dates/times displayed in activities will vary with your chosen time zone, however you are still bound to the 11:55 PM GMT-5
deadline.
Jump to...
Resources
UoPeople Library
Orientation Videos
LRC
Syllabus Repository
Honors Lists
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 28/29
1/8/22, 4:17 AM CS 1101 - AY2022-T2: Discussion Unit 8
Honors Lists
Links
About Us
Policies
University Catalog
Support
Student Portal
Faculty
Faculty Portal
CTEL Learning Community
Office 365
Tipalti
Contact us
English (en)
English (en)
العربية(ar)
https://my.uopeople.edu/mod/forum/discuss.php?d=640912 29/29