1,234
edits
Changes
→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>
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
</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()
</source>
=== 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>