Changes

Jump to: navigation, search

OPS435 Python Lab 5

3,317 bytes removed, 07:25, 21 February 2018
PART 1 - Reading Data From Files
== 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 object to us, this 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 followingfile manipulation:<source lang="python">dirf.read() # read all lines and stores in a stringf.readlines() # read all lines and stores in a listf.readline() # read first line, if run a second time it will read the second line, then thirdf.close() # close the opened file
</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 file object:<source lang="python">help(f.read) # help for reading all lines and stores in a stringhelp(f.readlines) # help for reading all lines and stores in a listhelp(f.readline) # help for reading first line, if run a second time it will read the second line, then thirdhelp(f.writable) # help for determining if a file is writablehelp(f.close) # help for closing 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 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>
 
<blockquote>{{Admon/tip|style="padding-left:25px"|Creating a List of Stripped Lines of Text|A common (and preferred) method to store a list of stripped text (by line) is to use a store file contents into a variable, then use a loop to grow (i.e. "append") each stripped line. There are a couple of important things to note:<ol><li>You need to initialize the list as empty (eg. '''new-list&#61;[]''')</li><li>Use the '''append()''' function to grow each line (using the '''strip()''' function) within the loop</li></ol>}}</blockquote>
=== Create a Python Script Demonstrating Reading Files ===
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>

Navigation menu