Changes

Jump to: navigation, search

OPS435 Python Lab 5

10 bytes removed, 12:25, 4 September 2017
PART 1 - Reading From Files
:Files are accessed through the use of '''file objects'''. An '''object''' is a '''storage location''' which stores data in the form of '''attributes''' (variables) and '''methods''' (functions). Creating our own objects will be covered later in investigation 3.
== PART 1 - Reading Data From Files ==
:'''Perform the Following Steps:'''
::#Launch your Centos VM, open a shell terminal (as a regular user) and start a new ipython3 session:<source lang="python">
ipython3
</source>
::#Create a new text file in the lab5 directory:<source lang="python">
%cd ~/ops435/lab5
%vim ~/ops435/lab5/data.txt
</source>
::#Place the following content inside the new file and save it:<source lang="python">
Hello World
This is the second line
Last line
</source>
::#Now lets write some python code to open this file for reading.<source lang="python">
f = open('data.txt', 'r')
</source>
::#The f is a new variable we are creating to store the file object. The open() function takes two string arguments, a path to a file, and a mode option 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. Before we use this object lets inspect it with dir().<source lang="python">
dir(f)
</source>
::#Lets inspect some of the functions we can use on this file object.<source lang="python">
help(f.read) # Reads all lines and stores in a string
help(f.readlines) # Reads all lines and stores in a list
f.closed # Object attribute (variable boolean)
</source>
::#Next read data from the file and close the file to free up resources.<source lang="python">
read_data = f.read()
read_data
</source>
::#Finally close the file to free up the computer resources. First lets check to see if the file is already closed.<source lang="python">
f.closed # This object attribute will give true if the file is closed and false if open
f.close() # This method will close the file
f.closed # Confirm the file is closed
</source>
::#All together the code for reading from a file should look like the following:<source lang="python">
f = open('data.txt', 'r') # Open file
read_data = f.read() # Read from file
f.closed # Confirm file is closed
</source>
::#Another way to read data from a file is using the with statement. The advantage here is that it will automatically close() the file when complete:<source lang="python">
with open('data.txt', 'r') as f: # Open file
read_data = f.read() # Read from file
f.closed # Confirm file is closed
</source>
::#Next lets read the data<source lang="python">
read_data
</source>
::#This displays the data from the file in a single long string. The end of each line in the file will show a '\n' which represents the newline character in a file. Split the line on the new-line characters, so more inspection can be done on a single line at a time.<source lang="python">
dir(read_data)
help(read_data.split)
list_of_lines
</source>
::#The above works, but it's not the best way to get a list of lines. The man benefit above is that the lines can be split in any way desired(on spaces, on periods, etc). The easiest way to just get a list of all lines can be done using the file object and it's methods.<source lang="python">
f = open('data.txt', 'r')
method1 = list(f)
method2
</source>
::#Finally, sometimes all that needs to be done is to print the file to the screen. In these simple cases, there is no need to create extra/large variables to store the data, instead just print it to the screen one line at a time. Doing one of the following methods will save computer resources while reading files.<source lang="python">
f = open('data.txt', 'r')
for line in f:
f.close()
</source>
::#The python print() function by default adds the new-line character to the end of the line. Using the end='' argument used in print replaces the '\n' at the end with nothing ''. This allows the print() to use the new-line characters found on each line of the file that was read. Though if desired you can always strip the new-line characters from any lines. The strip() function will remove all leading and trailing whitespace, which may help in processing some lines or data.<source lang="python">
f = open('data.txt', 'r')
for line in f:
13,420
edits

Navigation menu