1,760
edits
Changes
→PART 1 - Handling Errors
:'''NOTE:''' Since many tasks that system administrators deal with files, this is a crucial skill to understand.
== PYTHON REFERENCE ==
:In previous labs, you have been advised to make notes and use online references. This also relates to working with files and learning about objected oriented programming to help becoming . You may be "overwhelmed" with the volume of information involved in this lab.
:Below is a table with links to useful online Python reference sites (by category). You may find these references useful when performing assignments, etc.
== PART 1 - Reading Data From Files ==
:'''Perform the Following Steps:'''
:#Launch your Centos VM, open a shell terminal (as a regular user) and start Create a new ipython3 session:<source lang="python">ipython3</source>file for testing.:#Create a new text file in the '''lab5''' directory:<source lang="pythonbash">%cd ~/ops435/lab5%vim ~/ops435/lab5/data.txt
</source>
:#Place the following content inside the new text file and save it:<source lang="python">
Hello World
This is the second linerline
Third line
Last line
</source><br>In order to read data from a text file, we need to create a special storage area (or a '''"buffer"''') an object that will be used to access and storage the data in a file. This special storage has different names for various In some programming languages (like C) this is called a file descriptor, or a '''"file pointer"''' in the C programming language or as an '''"object"''' in object oriented programming languages).<br>In Python, we define an '''objectit''' to act as a buffer to help access the data contained within the file. To simplify things for now, you can think of s an '''object''' as a '''special variable'''. You will learn more about object oriented programming later in this lab.<br><br>:#Now lets write some python code from the ipython3 prompt to open this created file for reading. We will define and object called '''"f"''' in order to act as a buffer to help retrieve content from our text file. Issue the following:<source lang="python">
f = open('data.txt', 'r')
</source><br>The '''open()''' function takes two string arguments: a path to a file, and a mode option (to ask for reading, writing, appending, etc). The ''open()'' function will return a special file object to us, this file object will allow us to read the lines inside the file.<br><br>:#You may recall that we used Here are the '''dir()''' function to display library information most useful functions for our Python library (eg. '''dir(sys)''' ). This function can also display other elements such as attributes and methods for an object. Issue the following:<source lang="python">dir(f)</source>:#Although the '''dir(f)''' function displays a lot of information, we will focus on only a few elements. Separately issue the following commands in order to inspect some of the '''functions'''. '''attributes''' and '''methods''' that we can use with this text file objectmanipulation:<source lang="python">help(f.read() # help for reading read all lines and stores in a stringhelp(f.readlines() # help for reading read all lines and stores in a listhelp(f.readline() # help for reading read first line, if run a second time it will read the second line, then thirdhelp(f.writable) # help for determining if a file is writablehelpclose(f.close) # help for closing close the opened filef.writable() # Object method (confirm if file is writable)f.name # Object attribute (contains name of opened file)f.closed # Object attribute (confirm if file is closed)
</source>
:#Next, issue the following commands to read data from the buffer of the opened file and store the contents into a variable called "<code>read_data"</code>, and then confirm the contents of the variable "<code>read_data</code>:<source lang="python">
read_data = f.read()
print(read_data)</source><br>After you have completed accessing data within a file, you should '''close''' the file in order to free up the computer resources. It is sometimes useful to first confirm that the file is still open prior to closing it.<br><br>:#Issue the following object attribute will provide a boolean value to confirm that the file is still open (''true'' indicates closed and ''false'But really you should know - it' indicates open):<source lang="python">f.closed </source>:#Finally, issue the following object method and object attribute to close the file, and then verify s your code that the file has been successfully closedwould have opened it:<source lang="python">
f.close() # This method will close the file
read_data.split('\n') # Returns a list
list_of_lines = read_data.split('\n') # Saves returned list in variable
print(list_of_lines)
</source><br>Although the above sequence works, there are '''functions''' and '''methods''' the we can use with '''our object (called "f")''' to place lines from our file into a '''list'''. This would help to reduce code and is considered a more common method to store multiple lines or records within a list.<br><br>
:#Issue both method1 and method2 sequence of commands Try these two different means to see how you can store data into a list more efficiently:<source lang="python">
# METHOD 1:
method1 = list(f)
f.close()
print(method1)
# METHOD 2:
method2 = f.readlines()
f.close()
</source>
=== Create a Python Script Demonstrating Reading Files ===
#!/usr/bin/env python3
</source>
python3 lab5a.py
Hello World
</source>
import lab5a
file_name = 'data.txt'
print(lab5a.read_file_string(file_name))# Will print 'Hello World\nThis is the second line\nThird line\nLast line\n'print(lab5a.read_file_list(file_name))# Will print ['Hello World', 'This is the second line', 'Third line', 'Last line']
</source>
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
ls CheckLab5.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab5.py
python3 ./CheckLab5.py -f -v lab5a
</source>
<br><br>
:Up to this point, you have learned how to access text from a file. In this section, you will learn how to write text to a file. Writing data to a file is useful for creating new content in a file or updating (modifying) existing data contained within a file.
f = open('file1.txt', 'w')
</source>
ls -l file1.txt
</source>The '''ls -l''' command should work, but if you experience any problems, place a % sign in front of the command. This represents the '''alias''' for the ''ls -l'' command (i.e. '''%ls -l''').<br><br>To add lines of text to the file, you would can use the '''write()''' method for the '''file object'''. For safe file management, always Typically you end every line in a text file with the special character ''''\n'''' to represent a "new line". Multiple lines may also be placed inside a single write operation: simply put the special character ''''\n'''' wherever a line should end.<br><br>::#To demonstrate Try adding multiple lines, issue the following command:<source lang="python">
f.write('Line 1\nLine 2 is a little longer\nLine 3 is too\n')
</source>Once the '''write()''' operation method has been runcompleted, the final step would be is to '''close()''' the file. The file MUST be closed properly or else data will not consistently be written to the file. '''NOTE: Not closing a file can lead to corruption corrupted or not changes being mademissing file contents.''':<br><br>::#Issue the following command to close our file:<source lang="python">
f.close()
</source>
f = open('file2.txt', 'w')
f.write('Line 1\nLine 2 is a little longer\nLine 3 is as well\n')
f.close()
</source>
cat file2.txt
</source>
<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|Make Backup Copies of Your Data Files|Since you might make a mistake, and accidentally destroy file contents when writing to your file, it is highly recommended to make backup copies of your files prior to running your Python scripts. This can be particularly useful when performing any future assignment involving writing to files.}}</blockquote><blockquote style="margin-left:35px;">{{Admon/caution|style="padding-left:25px"|Make Backup Copies of ALL your files|Since you are now writing code that opens files for writing you may accidentally truncate the wrong file (like your assignment file, for example). Make regular backups of all your work. Just copy it to a USB stick or if you're bored - learn version control.}}</blockquote><br><ol style="margin-left:80px;"><li value="97">Issue the following command shell commands to backup both of your newly-created files and confirm backup:<source lang="pythonbash">
cp file1.txt file1.txt.bk
cp file2.txt file2.txt.bk
ls -l file*
</source></li>
<li>Let's demonstrate what can happen if you perform an incorrect write() operation. Issue the following commands:<source lang="python">
f = open('file2.txt', 'w')
</source><source lang="bash">cat file2.txt </source>You should notice that the previous content in your file2.txt file was destroyed. Why do you you think the previous data was destroyedis no longer there?<br></li><li>Issue the following commands to restore Restore your file from the backup and verify the backup restoration:<source lang="pythonbash">
cp file2.txt.bk file2.txt
cat file2.txt </source>In the event that the data in To avoid overwriting the contents of a file is important and should not be overwritten, we can '''append''' data to the end of the file instead. Use the option 'a' instead of 'w' to perform appending.:<br><br></li><li>To demonstrate, issue the following commands:<source lang="python">
f = open('file1.txt', 'a')
f.write('This is the 4th line\n')
f.write('Last line in file\n')
f.close()
</source>
<source lang="bash">
cat file1.txt
</source>The final thing to consider when writing to files is to make certain that the values being written are '''strings'''. This means that before trying to place integers, floats, lists, or dictionaries into a file, first either convert the value using '''str()''' function or extract the specific strings from items in the list.<br><br></li>
<li>To see how to In this example we convert a single number and all the numbers into in a list to strings before writing them to be stored into a file, issue the following commands:<source lang="python">
my_number = 1000
my_list = [1,2,3,4,5]
f.close()
</source></li>
<li>Issue the following command to confirm Confirm that the '''write()''' operation was successful.<source lang="pythonbash">cat file3.txt # or %cat file3.txt
</source></li>
</ol>
=== Create a Python Script Demonstrating Writing to Files ===
def append_file_string(file_name, string_of_lines):
# Takes two strings, appends the string to the end of the file
# Takes two strings, reads data from first file, writes data to new file, adds line number to new file
</source>
if __name__ == '__main__':
file1 = 'seneca1.txt'
</source>
:::'''Sample Run 1:'''<source>rm seneca1.txt seneca2.txt seneca3.txt./lab5b.py First LineSecond LineThird Line Line 1Line 2Line 3 1:Line 12:Line 23:Line 3 </source> :::'''Sample Run 12 (run second time):'''<source lang="python">
python3 lab5b.py
First Line
Second Line
Third Line
First Line
Second Line
</source>
import lab5b
file1 = 'seneca1.txt'
lab5b.read_file_string(file1)
# Will print 'First Line\nSecond Line\nThird Line\nFirst Line\nSecond Line\nThird Line\n'
lab5b.write_file_list(file2, list1)
lab5b.read_file_string(file2)
# Will print 'Line 1\nLine 2\nLine 3\n'
lab5b.copy_file_add_line_numbers(file2, file3)
lab5b.read_file_string(file3)
# Will print '1:Line 1\n2:Line 2\n3:Line 3\n'
</source>
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
ls CheckLab5.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab5.py
python3 ./CheckLab5.py -f -v lab5b
</source>
<br><br>
= INVESTIGATION 2: Exceptions and Error Handling =
:Running into errors in programming will be a common occurrence. You should expect that it will happen for any code that you write. In python when a an error occurs, the python runtime raises a python object called an '''exception, which represents the error that occured. These exceptions are raised when python is no long able to handle what the code is trying to do'''. This section will give the programmer the ability teach you to catch these exceptions when they happen and to allow the program to continue running, however in many cases it might be a good idea or to stop the program when an exception happens anywayexecution with a readable error message.
== PART 1 - Handling Errors ==
print('5' + 10)
</source>You should get an exception error similar to the following:<source lang="python">
---------------------------------------------------------------------------
</source><br>'''Question:''' According to the exception error message, what do you think caused the error?<br><br>
try:
print(5 + 10)
15
</source><br>You should notice that since there was NOT an error, the Python script performed the required task.<br><br>
try:
print(5 + 'ten')
At least one of the values is NOT an integer
</source>
f = open('filethatdoesnotexist', 'r')
</source>
try:
f = open('filethatdoesnotexist', 'r')
print('no file found')
</source><br>Multiple exceptions can also be caught at the same time, such as does not exist, is a directory, or we don't have permission. <br><br>
try:
f = open('filethatdoesnotexist', 'r')
f.write('hello world\n')
f.close()
except (FileNotFoundError, PermissionError, IsADirectoryIsADirectoryError):
print('failed to open file')
</source>
try:
f = open(abc, 'r')
print('unknown error occured')
raise
</source>When catching multiple exceptions, make certain to catch the <u>lowest</u> ones contained in the exception-hierarchy first. For example, if you put 'Exception' first, both 'OSError' and 'FileNotFoundError', would never get caught.<br><br>'''TIP:''' In python it's usually best to 'try:' and 'except:' code rather than to try and determine attempt to anticipate everything that could go wrong with logic and '''if ''' statements. For example, instead of checking to see if a file exists and we have read permissions, it can be better to just try and read the file and fail and catch any errors with 'OSError'.
=== Create a Python Script Which Handles Errors ===
#!/usr/bin/env python3
python3 lab5c.py
15
</source>
import lab5c
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
ls CheckLab5.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab5.py
python3 ./CheckLab5.py -f -v lab5c
</source>
<br><br>
::<span style="color:green;font-size:1.5em;">✓</span> Output of: <code>cat lab5a.py lab5b.py lab5c.py</code>
= Practice For QuizzesLAB REVIEW = # What is the purpose of a '''file object'''?# Write a Python command to '''open''' the text file called '''customers.txt''' for read-only operations.# Write Python code to efficiently store the contents of the file in question #2 as a large string (including new-line characters) called '''customer-data'''.# Write Python code to store the contents of the file in question #2 as a list, Tests, Midterm & Final Exam =removing the new-line characters.# What is the purpose of '''closing''' an open file? Write a Python command to close the file opened in question #2.# Write the Python command to confirm you successfully closed the '''customers.txt''' file in question #5. What is the returned status from that command to indicate that the file has been closed?# What is the difference between opening a file for '''writing''' data as opposed to opening a file for '''appending''' data? What can be the consequence if you don't understand the difference between writing and appending data?# Write a Python command to open the file '''customer-data.txt''' for writing data.# Write a Python command to save the text: '''customer 1: Guido van Rossum''' (including a new-line character) to the opened file called '''customer-data.txt'''# xBriefly explain the process writing a list as separate lines to an open file.# xWhat is the purpose of '''handling''' exception errors?# xWrite a Python script to prompt a user for the name of the file to open. Use exception error handling to provide an error message that the specific file name (display that exact name) does not exist; otherwise, open the file for reading and display the entire contents of the file (line-by-line). [[Category:OPS435-Python]]