Changes

Jump to: navigation, search

OPS435 Python Lab 5

282 bytes removed, 11:48, 8 September 2017
PART 1 - Reading Data From Files
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 it's an '''object''' to act as a buffer to help access the data contained within the file. 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 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 object to us, this object will allow us to read the lines inside the file.<br><br>
:#You may recall that we used 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 for an object. Issue the following:<source lang="python">
dir(f)
read_data = f.read()
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>
:#Issue the following object attribute will provide a boolean value to confirm that the file is still open (''true'' indicates closed and ''false'' indicates open):<source lang="python">
f.closed
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:<blockquote><br><span stylesource lang="font-family:courier;python">f = open('data.txt', 'r') # Open file<br>read_data = f.read() # Read from file<br>f.close() # Close file<br>f.closed # Confirm file is closed</span><br></blockquotesource><br>Another way to read data from a file is using the '''with''' looping statement. The advantage by using the ''with'' loop is that the file will automatically close when the data within the file has been completely read<br><br>
:#To demonstrate, issue the following code block:<source lang="python">
with open('data.txt', 'r') as f: # Open file

Navigation menu