Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 5

4,259 bytes removed, 08:26, 21 January 2020
no edit summary
<font color='red'>
'''** DO NOT USE - TO BE UPDATED FOR CENTOS 8.0 **'''
</font>
= LAB OBJECTIVES =
:So far, you have created Python scripts to prompt a user to input data from the keyboard. When creating Python scripts, you may also need to be able to process large volumes of information, or store processed data for further processing. The first investigation in this lab will focus on file management, opening files, saving data to files, and reading files.
:'''NOTE:''' Since many tasks that system administrators perform deal with files, this is a crucial skill to understand.
:During the first investigation, you will be explosed to some '''object oriented programming''' elements. Object oriented programming languages (such as Python) have great advantages or other programming languages. Some advantages include greater modularity for '''more effective troubleshooting''', the ability to '''re-use''' objects, and to provide '''greater flexibility''' to allow objects to handle unique situations from various data (for example a date in different formats).introduce the student to basic object oriented programming. To keep things simple, this investigation will focus more on the concept that objects are primarily used to store data and code :It is very important to provide logic in your Python script in case it '''encounters an error'''. An example would be an invalid path-name or trying to close a file that is already closed. The second investigation in this lab will teach you look into how to the Python interpreter '''handle exception errors''' (commonly referred to as "exception handling") so at run time, and learn how to write Python code that your Python script will run gracefully even when problems occur during program execution.
== 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>
Hello World
This is the second line
Third line
Last line
</source><br>In order to read data from a text file, we need to create an object that will be used to access the data in a file. In some programming languages (like C) this is called a file descriptor, or a file pointer. In Python, it's an '''object'''. To simplify things for now, you can think of an '''object''' as a '''special variable'''. You will learn more about object oriented programming in a later 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 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 for our Python library (eg. '''dir(sys)''' ). This function can also display other elements such as attributes and methods most useful functions 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. But really you should know - it's your code that would have opened it.<br><br>:#Look at the following object attribute (it is a boolean value) to confirm that the file is still open (''true'' indicates closed and ''false'' indicates open):<source lang="python">f.closed </source>:#Finally, close the file with the close() method, and then verify that the file has been successfully closed:<source lang="python">
f.close() # This method will close the file
f.closed # Confirm that the file is closed
</source><br>Let's take a moment to revisit the '''file read''' sequence. The following code sequence will open a file, store the contents of a file into a variable, close the file and provide confirmation that the file has been closed:<source lang="python">
f = open('data.txt', 'r') # Open file
read_data = f.read() # Read from file
f.close() # Close file</source>
:#Let's look at the contents of:<source lang="python">read_data</source><br>This command displays in this case contains the data from the file in a single <u>long</u> string. The end of each line in the file will show the special character ''''\n'''' which represents the '''newline character''' in a file used to separate lines (or records in a traditional "flat database file"). It would be convenient to '''split''' the line on the new-line characters, so each line can be stored into a separate array elements (or as an item in our case, a list!).<br><br>:#First get help on the '''split''' method:<source lang="python">dir(read_data)help(read_data.split)</source>:#Next, store Store the contents of our file into a list called '''list_of_lines''':<source lang="python">
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>
:#Try these two different means to store data into a list more efficiently:<source lang="python">
method1 = list(f)
f.close()
print(method1)
# METHOD 2:
method2 = f.readlines()
f.close()
method2</source><br>Here's another way to read a file. In this case you're reading one line at a time rather than the entire file at once. The result looks the same since you're printing each line after the other, but you could potentially perform some operations on each line separately instead of just printing them:<br><source lang="python">f = open('data.txt', 'r')for line in f: print(line, end='')f.close()</source><br>The python ''print()'' function by default adds the new-line character to the end of the line. Using the '''end=''' argument with the print() function replaces the '\n' at the end with nothing . But in this example the lines we're printing already have newline characters in the end so we're getting exactly the same output as we had input. Alternatively you can always strip the new-line characters from any lines. The strip() function will remove all leading and trailing white-space, which may help in processing some types of text data:<br><source lang="python">f = open('data.txt', 'r')for line in f: print(line.strip())f.close(method2)
</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>
::3. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
python3 ./CheckLab5.py -f -v lab5a
</source>
::4. Before proceeding, make certain that you identify any and all errors in lab5a.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.
<br><br>
<blockquote style="margin-left:35px;">{{Admon/caution|style="padding-left:25px"|Risk of Losing File Contents|A common problem that new Python programmers may encounter is to accidentally erase existing contents of a file when writing new data to a file. When opening files for writing (using the ''''w'''' open function option), Python assumes existing content in the file is no longer wanted and it's immediately deleted; therefore, if you wish to write data to a file but keep existing content, you need to use the open file option ''''a'''' (append new data to a file).}}</blockquote>
:#To start, open the ipython3 shell:<source lang="python">ipython3</source>When opening a file for writing, the ''''w'''' option is specified with the '''open()''' function. When the 'w' option is specified, - previous contents (existing) content inside the file are is deleted. This deletion takes place the moment the open() function is executed as opposed , not when writing to the actual writing processfile. If the file that is being written to doesn't exist, the file will be created upon the file opening process.<br><br>:#Let's Create a temporary Python file and open a non-existent data file (called file1.txt) for writing:<source lang="python">
f = open('file1.txt', 'w')
</source>
:#To confirm that the new file now exists and is empty, issue the following shell command:<sourcelang="bash">%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 can use the '''write()''' method for the '''file object'''. 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>
:#Try adding multiple lines:<source lang="python">
f.write('Line 1\nLine 2 is a little longer\nLine 3 is too\n')
</source>Once the '''write()''' method has completed, the final step 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 no changes being mademissing file contents.''':<br><source lang="python">
f.close()
</source>
:#Issue the following shell command to view View the contents of the file in the shell to make sure the data was written successfully:<sourcelang="bash">%cat file1.txt</source>You will now create a new file called file2.txt, but this time run multiple write() methods in sequence. You will often write to a file multiple times inside a loop:<br><source lang="python">
f = open('file2.txt', 'w')
f.write('Line 1\nLine 2 is a little longer\nLine 3 is as well\n')
f.close()
</source>
:#Issue the following shell command to confirm that the contents were written to file2.txt:<sourcelang="bash">%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="7">Issue the following shell commands to backup both of your newly-created files and confirm backup:<source lang="bash">
%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:<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 is no longer there?<br></li>
<li>Issue the following shell 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 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>
f.close()
</source></li>
<li>Issue the following shell command to confirm Confirm that the '''write()''' operation was successful.<source lang="pythonbash">%cat file3.txt
</source></li>
</ol>
:::'''Sample Run 1:'''<source>
rm seneca1.txt seneca2.txt seneca3.txt
./lab5b.py
First Line
Second Line
Third Line
 
Line 1
Line 2
Line 3
 
1:Line 1
2:Line 2
3:Line 3
 
</source>
 
:::'''Sample Run 2 (run second time):'''<source>
python3 lab5b.py
First Line
Second Line
Third Line
First Line
Second Line
</source>
:::'''Sample Run 2 3 (with import):'''<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>
::3. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
python3 ./CheckLab5.py -f -v lab5b
</source>
::4. Before proceeding, make certain that you identify any and all errors in lab5b.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.
<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 ==
:According to Wikipedia, Exception Handling There is ''"the process of responding to the occurrence, during computation, a <u>massive amount</u> of exceptions – anomalous or exceptional conditions requiring special processing – often changing . Online references can be useful. If you are searching for a common exception check out the normal flow of program execution."''<br>( [https://endocs.wikipediapython.org/wiki3/library/Exception_handling) exceptions.html#exception-hierarchy Python Exception Documentation.]
:There are a <u>massive amount</u> In this section, we will provide examples of exceptions (way too many how to cover in this course). On the other hand, online references can be useful. If you are searching for handle a specific exception check out the [https://docs.python.org/3/library/few exceptions.html#exception-hierarchy when creating Python Exception Documentationscripts.]
:In this section, we will provide examples of how to handle a few exceptions when creating Python scripts. :'''Perform the Following Steps:'''::#To start, open the ipython3 shell:<source lang="python">ipython3</source>. Before attempting to handle exception errors, let's create an error, and then see how to can "handle" the exception error.it:<br><br>::#Issue the following to create an exception error:<source lang="python">
print('5' + 10)
</source>You should get an exception error similar to the following:<source lang="python">
---------------------------------------------------------------------------
TypeError Traceback (most recent call last) Fiel "<ipython-input-3-1b929b80ca50stdin> ", line 1, in <module>()----> 1 print(TypeError: Can't convert '5int' + 10)object to str implicitly
TypeError: Can't convert 'int' object to str implicitly
</source><br>'''Question:''' According to the exception error message, what do you think caused the error?<br><br>
::#Click on the link '[https://docs.python.org/3/library/exceptions.html#concrete-exceptions https://docs.python.org/3/library/exceptions.html#concrete-exceptions.]' and scroll or search for '''TypeError'''. Take a few moments to determine what a ''TypeError'' exception error means.<br><br>You should have learned that the TypeError exception error indicates a mismatch of a type (i.e. string, int, float, list, etc). If Python doesn't know how to handle it, perhaps we could change the number into a string or change the string into a number or at least provide a more user-friendly error message.<br><br>If we don't want the user of our program to have to write this program safelylearn how to read Python exceptions (which is a very good idea), we can catch/trap/handle this error while when it's happeninghappens. This is done with a specific block of code called a [https://docs.python.org/3/tutorial/errors.html#handling-exceptions '''try clause'''] where you place code in-between the '''try:''' and the '''except:''' coding blocks. In a general sense, it works like a modified if-else statement, where the try statement acts as a test, and the except statement will or will not handle the exception depending if it occurs or does NOT occur. That is to say, If no error occurs in the code contained in the '''except''' section, the script will continue as usual but if an error occurs in the except section, then it can be handled with additional coding (like an user-friendly error message).<br>Let's demonstrate to handle our TypeError error by issuing with code that first does not contain an error and then re-issue similar code that DOES generate an error.<br><br>::#Issue the The following code that does NOT generate an error:<source lang="python">
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>
::#Now, issue the The following code that handles an exception error to provide user-friendly feedback that at least one of the values is not an integer:<source lang="python">
try:
print(5 + 'ten')
At least one of the values is NOT an integer
</source>
::#Let's generate another type of error where we try to open a file that doesn't exist:<source lang="python">
f = open('filethatdoesnotexist', 'r')
</source>
::#Now, to catch and handle this exception error, issue the following lines of code:<source lang="python">
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>
::#To test out the error handling code (previously issued), try removing permissions from the file, or specify a directory instead of a regular file, and then try to open it:<source lang="python">
try:
f = open('filethatdoesnotexist', 'r')
f.write('hello world\n')
f.close()
except (FileNotFoundError, PermissionError, IsADirectoryIsADirectoryError):
print('failed to open file')
</source>
::#By taking the time to view the [https://docs.python.org/3/library/exceptions.html#exception-hierarchy Python Exception Hierarchy], you can see how errors get caught in python. The options '''FileNotFoundError''', '''PermissionError''', and '''IsADirectory''' are all inherited from '''OSError'''. This means that while using more specific errors might be useful for better error messages and handling, it's not always possible to catch every error all the time.::#Another way to catch multiple exceptions can be performed by issuing the following is with separate <code>except</code> bloks:<source lang="python">
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 ===
:'''Perform the Following Instructions'''::#Create the '''~/ops435/lab5/lab5c.py''' script.::#Use the following as a template:<source lang="python">
#!/usr/bin/env python3
::::'''Sample Run 1:'''<source lang="python">
python3 lab5c.py
15
</source>
::::'''Sample Run 2 (with import):'''<source lang="python">
import lab5c
:::3. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
python3 ./CheckLab5.py -f -v lab5c
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab5c.py. When the checking script tells you everything is OK before proceeding to the next step.
<br><br>
# What is the purpose of '''handling''' exception errors?
# Write 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]]
1,760
edits