Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 5

243 bytes added, 16:04, 4 September 2017
PART 1 - Reading Data From Files
:#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 store the retrieved content from our text file. Issue the following:<source lang="python">
f = open('data.txt', 'r')
</source>:#The f is a new variable we are creating to store the file object. <br>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 <br><br>As mentioned above, objects are considered special variables that can contain attributes and methods. We can obtain these attributes and methods by using the dir() function. Does this seem familiar? It should since we use this object lets inspect it with used the dir()function in a previous lab, which showed us the attributes and methods for another object.Issue the following:<source lang="python">
dir(f)
</source>
13,420
edits