IHE Python course, 2017

Reading text files

T.N.Olsthoorn, Feb 27, 2017

Reading and writing files is one of the essential functions of computers as this is the connection between volatile data in the computer's memory and permanent data on disk, in the cloud.

The three forms of reading text files are shown, and we'll illustrate handling its contents. We'll take the README.md file as an example as it is readily available on this site.

Finding the file in the directory tree

First steps are finding the file in the directory structure. We'll start with an illutration how this can be done using some functions in the os module, which deal with directories.


In [1]:
import os

In [2]:
os.listdir()  # make a list of the files in the current directory, so that we may handle them.


Out[2]:
['.DS_Store',
 '.ipynb_checkpoints',
 'dealingWithStrings.ipynb',
 'introspection.ipynb',
 'readingText.ipynb']

Turn the relative refference of the current directory, '.' into an absolute path.

Then specify the file name that we're looking for.

And in a while-loop look upward in an ever higher diectory in the directory tree until we found that with our file.

Walking upward in the tree is done by cutting off the tail of the path in each step.


In [3]:
apth = os.path.abspath('.')

fname = 'README.md'

print("Starting in folder <{}>,\n to look for file <{}> ...".format(apth, fname))
print()

print("I'm searching: ...")
while not fname in os.listdir(apth):
    apth = apth.rpartition(os.sep)[0]
    print(apth)
    
if fname in os.listdir(apth):
    print("... Yep, got'm!")
else:
    print("... H'm, missed him")
    
print("\nOk, file <{}> is in folder: ".format(fname), apth)
print('\nHere is the list of files in this folder:')
os.listdir(apth)


Starting in folder </Users/Theo/GRWMODELS/Python_projects/IHEcourse2017/exercises/Mar07>,
 to look for file <README.md> ...

I'm searching: ...
/Users/Theo/GRWMODELS/Python_projects/IHEcourse2017/exercises
/Users/Theo/GRWMODELS/Python_projects/IHEcourse2017
... Yep, got'm!

Ok, file <README.md> is in folder:  /Users/Theo/GRWMODELS/Python_projects/IHEcourse2017

Here is the list of files in this folder:
Out[3]:
['.DS_Store',
 '.git',
 '.gitignore',
 '.ipynb_checkpoints',
 'exercises',
 'IMG_20170221_155447496.jpg',
 'IntroPythonCourse.pptx',
 'LICENSE',
 'logo',
 'mbakker7-exploratory_computing_with_python-ca15088',
 'photos',
 'README.md',
 'sandbox']

Reading the file (at once)

When we know where the file is, we can open it for reading.

We have to open it, which yields a reader object, by which we can read the file.

reader = open(path, 'r')

s = reader.read()

reader.close()

Problem with this, is that when we are exploring the reader, we may easily reach the end of file after which nothing more is read and s is an empty string. Furthermore, when we experiment, we may easily open the same file many times and forget to close it.

The with statement is a solution to that, because it automatically closes the file when we finish its block.

With the with statement we may read the entire file into a string like so.


In [4]:
with open(os.path.join(apth, fname), 'r') as reader:
    s = reader.read()

It's the read that swallows the entire file at once and dumps its contents in the string s.

Check if the reader in, indeed, closed after we finished the with block:


In [5]:
reader.closed


Out[5]:
True

Then show the contenct of the sring s:


In [6]:
print(s)


# IHE-python-course-2017
Material for the extracurricular python course given at UNESCO-IHE from 21-Feb-2017

Upon request of the virtually entire student community studying at UNESCO-IHE in Delft in 2017, an extracurricular course on Python was given as it was realized that learning python is an essential asset for future engineers and scientists.

Next to my own exercises, we'll borrow much of the exercises Prof. Mark Bakker's tutorials for all students at the faculty of CEGS at TUDelft, which are availalbe on github. Other material from the internet may also be used.

The focus is on the needs of scientists and engineers.

The students are supposed to install the Anaconda packages on their laptop and the lessons will be on Tuesdays 16:45-17:30 as from Feb 21, 2017 in the auditorium of IHE at the Westvest in Delft.

For your own practicing I advice the exercises in the book (2015) **"Automate the Boring Stuff with Python"** by Al Sweigart. The book can be read free on the Internet. The excercises are as simple as possible, systematically aranged, in each step explaining one new aspect, everything in it is extremely clearly explained and the book is even relaxed reading. Each chapter ends with a set of questions and one or two practical projects that are interesting and useful by themselves. In one word, the book is a wealth. I assume that students will do such exercises themselves, so that we can taken Python a step further in class.

Theo Olsthoorn, Prof. groundwater
Delft Feb. 20, 2017

**Feb 21:**
Introduction, see ppt presentation on this site.
MB's first notebook was used as a start thereafter. Basic plotting of polynomicals using matplotlib.pyplot and numpy. The resulting notebook is on this site.

**Feb 28**
This time we'll heavily focus on tuples, lists, dicts and sets. We will read data from an excel workbook, turn this into dicts. We'll make heavily use of the list, dict and set comprehensions to generate the lists and dict that we need to order, join, analyze and visualize the results from the data that we have read in from different sheets of the excel workbook. Hence there will be quite some advanced working with sequences, iterators and generators so that afterwards you should feel comfortable with them.

Counting words and phrases

Now that we have the entire file read into a single string, s, we can just as well analyze it a bit, by counting the number of words, letters, and the frequency of each letter.

just split the sting in workds based on whitespace and count their number.


In [7]:
print("There are {} words in file {}".format(len(s.split(sep=' ')), fname))


There are 362 words in file README.md

We might estimate the number of sentences by counting the number of periods '.'

One way is to use the . as a separator:


In [8]:
nPhrases = len(s.split(sep='.'))  # also works without the keyword sep

print("We find {} phrases in file {}".format(nPhrases, fname))


We find 24 phrases in file README.md

We could just as wel count the number of dots in s directly, using one of the string methods, in this case s.count()


In [9]:
print("There are {} dots in file {}".format(s.count('.'), fname))


There are 23 dots in file README.md

Counting the non-whitespace characters

Now let's see how many non-whitespace characters there are in s.

A coarse way to remove whitespace would be splitting s and rejoining the obtained list of words without any whitespace like so:


In [14]:
s1 = "".join(s.split()).lower()  # also make all letters in lowerface()
print(s1)


#ihe-python-course-2017materialfortheextracurricularpythoncoursegivenatunesco-ihefrom21-feb-2017uponrequestofthevirtuallyentirestudentcommunitystudyingatunesco-iheindelftin2017,anextracurricularcourseonpythonwasgivenasitwasrealizedthatlearningpythonisanessentialassetforfutureengineersandscientists.nexttomyownexercises,we'llborrowmuchoftheexercisesprof.markbakker'stutorialsforallstudentsatthefacultyofcegsattudelft,whichareavailalbeongithub.othermaterialfromtheinternetmayalsobeused.thefocusisontheneedsofscientistsandengineers.thestudentsaresupposedtoinstalltheanacondapackagesontheirlaptopandthelessonswillbeontuesdays16:45-17:30asfromfeb21,2017intheauditoriumofiheatthewestvestindelft.foryourownpracticingiadvicetheexercisesinthebook(2015)**"automatetheboringstuffwithpython"**byalsweigart.thebookcanbereadfreeontheinternet.theexcercisesareassimpleaspossible,systematicallyaranged,ineachstepexplainingonenewaspect,everythinginitisextremelyclearlyexplainedandthebookisevenrelaxedreading.eachchapterendswithasetofquestionsandoneortwopracticalprojectsthatareinterestingandusefulbythemselves.inoneword,thebookisawealth.iassumethatstudentswilldosuchexercisesthemselves,sothatwecantakenpythonastepfurtherinclass.theoolsthoorn,prof.groundwaterdelftfeb.20,2017**feb21:**introduction,seepptpresentationonthissite.mb'sfirstnotebookwasusedasastartthereafter.basicplottingofpolynomicalsusingmatplotlib.pyplotandnumpy.theresultingnotebookisonthissite.**feb28**thistimewe'llheavilyfocusontuples,lists,dictsandsets.wewillreaddatafromanexcelworkbook,turnthisintodicts.we'llmakeheavilyuseofthelist,dictandsetcomprehensionstogeneratethelistsanddictthatweneedtoorder,join,analyzeandvisualizetheresultsfromthedatathatwehavereadinfromdifferentsheetsoftheexcelworkbook.hencetherewillbequitesomeadvancedworkingwithsequences,iteratorsandgeneratorssothatafterwardsyoushouldfeelcomfortablewiththem.

All characters in a list

If we convert a string into a list, we get the list of its individual characters.


In [15]:
list(s1)


Out[15]:
['#',
 'i',
 'h',
 'e',
 '-',
 'p',
 'y',
 't',
 'h',
 'o',
 'n',
 '-',
 'c',
 'o',
 'u',
 'r',
 's',
 'e',
 '-',
 '2',
 '0',
 '1',
 '7',
 'm',
 'a',
 't',
 'e',
 'r',
 'i',
 'a',
 'l',
 'f',
 'o',
 'r',
 't',
 'h',
 'e',
 'e',
 'x',
 't',
 'r',
 'a',
 'c',
 'u',
 'r',
 'r',
 'i',
 'c',
 'u',
 'l',
 'a',
 'r',
 'p',
 'y',
 't',
 'h',
 'o',
 'n',
 'c',
 'o',
 'u',
 'r',
 's',
 'e',
 'g',
 'i',
 'v',
 'e',
 'n',
 'a',
 't',
 'u',
 'n',
 'e',
 's',
 'c',
 'o',
 '-',
 'i',
 'h',
 'e',
 'f',
 'r',
 'o',
 'm',
 '2',
 '1',
 '-',
 'f',
 'e',
 'b',
 '-',
 '2',
 '0',
 '1',
 '7',
 'u',
 'p',
 'o',
 'n',
 'r',
 'e',
 'q',
 'u',
 'e',
 's',
 't',
 'o',
 'f',
 't',
 'h',
 'e',
 'v',
 'i',
 'r',
 't',
 'u',
 'a',
 'l',
 'l',
 'y',
 'e',
 'n',
 't',
 'i',
 'r',
 'e',
 's',
 't',
 'u',
 'd',
 'e',
 'n',
 't',
 'c',
 'o',
 'm',
 'm',
 'u',
 'n',
 'i',
 't',
 'y',
 's',
 't',
 'u',
 'd',
 'y',
 'i',
 'n',
 'g',
 'a',
 't',
 'u',
 'n',
 'e',
 's',
 'c',
 'o',
 '-',
 'i',
 'h',
 'e',
 'i',
 'n',
 'd',
 'e',
 'l',
 'f',
 't',
 'i',
 'n',
 '2',
 '0',
 '1',
 '7',
 ',',
 'a',
 'n',
 'e',
 'x',
 't',
 'r',
 'a',
 'c',
 'u',
 'r',
 'r',
 'i',
 'c',
 'u',
 'l',
 'a',
 'r',
 'c',
 'o',
 'u',
 'r',
 's',
 'e',
 'o',
 'n',
 'p',
 'y',
 't',
 'h',
 'o',
 'n',
 'w',
 'a',
 's',
 'g',
 'i',
 'v',
 'e',
 'n',
 'a',
 's',
 'i',
 't',
 'w',
 'a',
 's',
 'r',
 'e',
 'a',
 'l',
 'i',
 'z',
 'e',
 'd',
 't',
 'h',
 'a',
 't',
 'l',
 'e',
 'a',
 'r',
 'n',
 'i',
 'n',
 'g',
 'p',
 'y',
 't',
 'h',
 'o',
 'n',
 'i',
 's',
 'a',
 'n',
 'e',
 's',
 's',
 'e',
 'n',
 't',
 'i',
 'a',
 'l',
 'a',
 's',
 's',
 'e',
 't',
 'f',
 'o',
 'r',
 'f',
 'u',
 't',
 'u',
 'r',
 'e',
 'e',
 'n',
 'g',
 'i',
 'n',
 'e',
 'e',
 'r',
 's',
 'a',
 'n',
 'd',
 's',
 'c',
 'i',
 'e',
 'n',
 't',
 'i',
 's',
 't',
 's',
 '.',
 'n',
 'e',
 'x',
 't',
 't',
 'o',
 'm',
 'y',
 'o',
 'w',
 'n',
 'e',
 'x',
 'e',
 'r',
 'c',
 'i',
 's',
 'e',
 's',
 ',',
 'w',
 'e',
 "'",
 'l',
 'l',
 'b',
 'o',
 'r',
 'r',
 'o',
 'w',
 'm',
 'u',
 'c',
 'h',
 'o',
 'f',
 't',
 'h',
 'e',
 'e',
 'x',
 'e',
 'r',
 'c',
 'i',
 's',
 'e',
 's',
 'p',
 'r',
 'o',
 'f',
 '.',
 'm',
 'a',
 'r',
 'k',
 'b',
 'a',
 'k',
 'k',
 'e',
 'r',
 "'",
 's',
 't',
 'u',
 't',
 'o',
 'r',
 'i',
 'a',
 'l',
 's',
 'f',
 'o',
 'r',
 'a',
 'l',
 'l',
 's',
 't',
 'u',
 'd',
 'e',
 'n',
 't',
 's',
 'a',
 't',
 't',
 'h',
 'e',
 'f',
 'a',
 'c',
 'u',
 'l',
 't',
 'y',
 'o',
 'f',
 'c',
 'e',
 'g',
 's',
 'a',
 't',
 't',
 'u',
 'd',
 'e',
 'l',
 'f',
 't',
 ',',
 'w',
 'h',
 'i',
 'c',
 'h',
 'a',
 'r',
 'e',
 'a',
 'v',
 'a',
 'i',
 'l',
 'a',
 'l',
 'b',
 'e',
 'o',
 'n',
 'g',
 'i',
 't',
 'h',
 'u',
 'b',
 '.',
 'o',
 't',
 'h',
 'e',
 'r',
 'm',
 'a',
 't',
 'e',
 'r',
 'i',
 'a',
 'l',
 'f',
 'r',
 'o',
 'm',
 't',
 'h',
 'e',
 'i',
 'n',
 't',
 'e',
 'r',
 'n',
 'e',
 't',
 'm',
 'a',
 'y',
 'a',
 'l',
 's',
 'o',
 'b',
 'e',
 'u',
 's',
 'e',
 'd',
 '.',
 't',
 'h',
 'e',
 'f',
 'o',
 'c',
 'u',
 's',
 'i',
 's',
 'o',
 'n',
 't',
 'h',
 'e',
 'n',
 'e',
 'e',
 'd',
 's',
 'o',
 'f',
 's',
 'c',
 'i',
 'e',
 'n',
 't',
 'i',
 's',
 't',
 's',
 'a',
 'n',
 'd',
 'e',
 'n',
 'g',
 'i',
 'n',
 'e',
 'e',
 'r',
 's',
 '.',
 't',
 'h',
 'e',
 's',
 't',
 'u',
 'd',
 'e',
 'n',
 't',
 's',
 'a',
 'r',
 'e',
 's',
 'u',
 'p',
 'p',
 'o',
 's',
 'e',
 'd',
 't',
 'o',
 'i',
 'n',
 's',
 't',
 'a',
 'l',
 'l',
 't',
 'h',
 'e',
 'a',
 'n',
 'a',
 'c',
 'o',
 'n',
 'd',
 'a',
 'p',
 'a',
 'c',
 'k',
 'a',
 'g',
 'e',
 's',
 'o',
 'n',
 't',
 'h',
 'e',
 'i',
 'r',
 'l',
 'a',
 'p',
 't',
 'o',
 'p',
 'a',
 'n',
 'd',
 't',
 'h',
 'e',
 'l',
 'e',
 's',
 's',
 'o',
 'n',
 's',
 'w',
 'i',
 'l',
 'l',
 'b',
 'e',
 'o',
 'n',
 't',
 'u',
 'e',
 's',
 'd',
 'a',
 'y',
 's',
 '1',
 '6',
 ':',
 '4',
 '5',
 '-',
 '1',
 '7',
 ':',
 '3',
 '0',
 'a',
 's',
 'f',
 'r',
 'o',
 'm',
 'f',
 'e',
 'b',
 '2',
 '1',
 ',',
 '2',
 '0',
 '1',
 '7',
 'i',
 'n',
 't',
 'h',
 'e',
 'a',
 'u',
 'd',
 'i',
 't',
 'o',
 'r',
 'i',
 'u',
 'm',
 'o',
 'f',
 'i',
 'h',
 'e',
 'a',
 't',
 't',
 'h',
 'e',
 'w',
 'e',
 's',
 't',
 'v',
 'e',
 's',
 't',
 'i',
 'n',
 'd',
 'e',
 'l',
 'f',
 't',
 '.',
 'f',
 'o',
 'r',
 'y',
 'o',
 'u',
 'r',
 'o',
 'w',
 'n',
 'p',
 'r',
 'a',
 'c',
 't',
 'i',
 'c',
 'i',
 'n',
 'g',
 'i',
 'a',
 'd',
 'v',
 'i',
 'c',
 'e',
 't',
 'h',
 'e',
 'e',
 'x',
 'e',
 'r',
 'c',
 'i',
 's',
 'e',
 's',
 'i',
 'n',
 't',
 'h',
 'e',
 'b',
 'o',
 'o',
 'k',
 '(',
 '2',
 '0',
 '1',
 '5',
 ')',
 '*',
 '*',
 '"',
 'a',
 'u',
 't',
 'o',
 'm',
 'a',
 't',
 'e',
 't',
 'h',
 'e',
 'b',
 'o',
 'r',
 'i',
 'n',
 'g',
 's',
 't',
 'u',
 'f',
 'f',
 'w',
 'i',
 't',
 'h',
 'p',
 'y',
 't',
 'h',
 'o',
 'n',
 '"',
 '*',
 '*',
 'b',
 'y',
 'a',
 'l',
 's',
 'w',
 'e',
 'i',
 'g',
 'a',
 'r',
 't',
 '.',
 't',
 'h',
 'e',
 'b',
 'o',
 'o',
 'k',
 'c',
 'a',
 'n',
 'b',
 'e',
 'r',
 'e',
 'a',
 'd',
 'f',
 'r',
 'e',
 'e',
 'o',
 'n',
 't',
 'h',
 'e',
 'i',
 'n',
 't',
 'e',
 'r',
 'n',
 'e',
 't',
 '.',
 't',
 'h',
 'e',
 'e',
 'x',
 'c',
 'e',
 'r',
 'c',
 'i',
 's',
 'e',
 's',
 'a',
 'r',
 'e',
 'a',
 's',
 's',
 'i',
 'm',
 'p',
 'l',
 'e',
 'a',
 's',
 'p',
 'o',
 's',
 's',
 'i',
 'b',
 'l',
 'e',
 ',',
 's',
 'y',
 's',
 't',
 'e',
 'm',
 'a',
 't',
 'i',
 'c',
 'a',
 'l',
 'l',
 'y',
 'a',
 'r',
 'a',
 'n',
 'g',
 'e',
 'd',
 ',',
 'i',
 'n',
 'e',
 'a',
 'c',
 'h',
 's',
 't',
 'e',
 'p',
 'e',
 'x',
 'p',
 'l',
 'a',
 'i',
 'n',
 'i',
 'n',
 'g',
 'o',
 'n',
 'e',
 'n',
 'e',
 'w',
 'a',
 's',
 'p',
 'e',
 'c',
 't',
 ',',
 'e',
 'v',
 'e',
 'r',
 'y',
 't',
 'h',
 'i',
 'n',
 'g',
 'i',
 'n',
 'i',
 't',
 'i',
 's',
 'e',
 'x',
 't',
 'r',
 'e',
 'm',
 'e',
 'l',
 'y',
 'c',
 'l',
 'e',
 'a',
 'r',
 'l',
 'y',
 'e',
 'x',
 'p',
 'l',
 'a',
 'i',
 'n',
 'e',
 'd',
 'a',
 'n',
 'd',
 't',
 'h',
 'e',
 'b',
 'o',
 'o',
 'k',
 'i',
 's',
 'e',
 'v',
 'e',
 'n',
 'r',
 'e',
 'l',
 'a',
 'x',
 'e',
 'd',
 'r',
 'e',
 'a',
 'd',
 'i',
 'n',
 'g',
 '.',
 'e',
 'a',
 'c',
 'h',
 'c',
 'h',
 'a',
 'p',
 't',
 ...]

The unique characters in a set

By turning the string into a set, we get the set of its unique characters:


In [16]:
set(s1)


Out[16]:
{'"',
 '#',
 "'",
 '(',
 ')',
 '*',
 ',',
 '-',
 '.',
 '0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 ':',
 'a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g',
 'h',
 'i',
 'j',
 'k',
 'l',
 'm',
 'n',
 'o',
 'p',
 'q',
 'r',
 's',
 't',
 'u',
 'v',
 'w',
 'x',
 'y',
 'z'}

The number of occurences of each non-white character in the file

To count the frequency of each character we could use those from the set as keys in a dict. We can generate the dict with the frequency if each character in a dict comprehension that combines the unique letter as a key with the method count(key) applied on s1, the string without whitespace:


In [17]:
ccnt = {c : s1.count(c) for c in set(s1)}
pprint(ccnt)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-0c35a1c774f4> in <module>()
      1 ccnt = {c : s1.count(c) for c in set(s1)}
----> 2 pprint(ccnt)

NameError: name 'pprint' is not defined

Lets order the letters after their frequency of occurrence in the file:

We can do so in one line, but this needs some explanaion.

First we generate a list from the dict in which each item is a list of 2 itmes namely [char, number]

Second we apply sorted on that list to get a sorted list. But we don't want it to be sorted based on the character, but based on the number. Therfore, we use the key argument. It tels that each item has to be compared on the second value (lambda x: x[1]).

Finally, this yields the list that we want, but with the largest frequency at the bottom. So we turn this list upside down by using the slice [::-1] at the end.

Here it is:


In [159]:
sorted([[k, ccnt[k]] for k in ccnt.keys()], key=lambda x: x[1])[::-1]


Out[159]:
[['e', 236],
 ['t', 186],
 ['s', 140],
 ['a', 131],
 ['o', 122],
 ['n', 120],
 ['i', 118],
 ['r', 102],
 ['h', 78],
 ['l', 77],
 ['u', 56],
 ['c', 56],
 ['d', 55],
 ['f', 46],
 ['p', 36],
 ['w', 33],
 ['m', 32],
 ['b', 29],
 ['y', 28],
 ['g', 24],
 ['.', 23],
 [',', 19],
 ['k', 17],
 ['v', 15],
 ['x', 14],
 ['*', 12],
 ['2', 11],
 ['1', 11],
 ['0', 8],
 ['-', 8],
 ['7', 6],
 ["'", 5],
 ['q', 4],
 [':', 3],
 ['z', 3],
 ['5', 2],
 ['j', 2],
 ['"', 2],
 ['#', 1],
 ['4', 1],
 ['8', 1],
 ['3', 1],
 ['6', 1],
 ['(', 1],
 [')', 1]]

Reading the file and returning a list of strings, one per line

For this we would read reader.readlines() instead of reader.read:


In [18]:
with open(os.path.join(apth, fname), 'r') as reader:
    s = reader.readlines()

type(s)


Out[18]:
list

In [19]:
pprint(s)


Pretty printing has been turned OFF

From this point onward, you can analyse each line in sequence, pick out lines, etc.

Reading a single line and lines one by one

Often you don't want to read the entire file into memory (into a single character) at once. It might blow up the computer's memory if the file size were gigabits, as can easily the case with output of some models. And if it wouldn't crash the memory, your pc may still become very slow with large files. So a better and more generally applied way to read in a file is line by line, based on the newline characters that are embedded in them.

In that case you can read the file in line by line, one at a time, not using reader.read() or reader.readlines() but reader.readline()


In [191]:
with open(os.path.join(apth, fname), 'r') as reader:
    s = reader.readline()

type(s)


Out[191]:
str

In [192]:
print(s)


# IHE-python-course-2017

Which yields a string, the first string of the file in this case.

The problem is now, that no more lines can be read from this file, because with the with statement, the file closes automatically as soon as the python reaches the end of its block:


In [193]:
s = reader.readline()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-193-8850e808884b> in <module>()
----> 1 s = reader.readline()

ValueError: I/O operation on closed file.

Therefore, we should not use the with statement and hand-close the file when we're done, or put anything that we do with the strings that we read inside the with block.

We may be tempted to put the reader in a while-loop like so

s=[] while True: s.append(reader.readline())

But don't do that, becaus the while-loop will never end


In [194]:
with open(os.path.join(apth, fname), 'r') as reader:
    lines = []
    while True:
        s = reader.readline()
        if s=="":
            break
        lines.append(s)

pprint(lines)


['# IHE-python-course-2017\n',
 'Material for the extracurricular python course given at UNESCO-IHE from '
 '21-Feb-2017\n',
 '\n',
 'Upon request of the virtually entire student community studying at '
 'UNESCO-IHE in Delft in 2017, an extracurricular course on Python was given '
 'as it was realized that learning python is an essential asset for future '
 'engineers and scientists.\n',
 '\n',
 "Next to my own exercises, we'll borrow much of the exercises Prof. Mark "
 "Bakker's tutorials for all students at the faculty of CEGS at TUDelft, which "
 'are availalbe on github. Other material from the internet may also be '
 'used.\n',
 '\n',
 'The focus is on the needs of scientists and engineers.\n',
 '\n',
 'The students are supposed to install the Anaconda packages on their laptop '
 'and the lessons will be on Tuesdays 16:45-17:30 as from Feb 21, 2017 in the '
 'auditorium of IHE at the Westvest in Delft.\n',
 '\n',
 'For your own practicing I advice the exercises in the book (2015) '
 '**"Automate the Boring Stuff with Python"** by Al Sweigart. The book can be '
 'read free on the Internet. The excercises are as simple as possible, '
 'systematically aranged, in each step explaining one new aspect, everything '
 'in it is extremely clearly explained and the book is even relaxed reading. '
 'Each chapter ends with a set of questions and one or two practical projects '
 'that are interesting and useful by themselves. In one word, the book is a '
 'wealth. I assume that students will do such exercises themselves, so that we '
 'can taken Python a step further in class.\n',
 '\n',
 'Theo Olsthoorn, Prof. groundwater\n',
 'Delft Feb. 20, 2017\n',
 '\n',
 '**Feb 21:**\n',
 'Introduction, see ppt presentation on this site.\n',
 "MB's first notebook was used as a start thereafter. Basic plotting of "
 'polynomicals using matplotlib.pyplot and numpy. The resulting notebook is on '
 'this site.\n',
 '\n',
 '**Feb 28**\n',
 "This time we'll heavily focus on tuples, lists, dicts and sets. We will read "
 "data from an excel workbook, turn this into dicts. We'll make heavily use of "
 'the list, dict and set comprehensions to generate the lists and dict that we '
 'need to order, join, analyze and visualize the results from the data that we '
 'have read in from different sheets of the excel workbook. Hence there will '
 'be quite some advanced working with sequences, iterators and generators so '
 'that afterwards you should feel comfortable with them.\n']

In [195]:
reader.readline?

Of course, there is much, much more, but this is probably the most important base knowledge about file reading. File writing of textfile is straightforward. You open a file with open( fname, 'w') for writing or open(fname,'a') for appending and you can start writing lines to it. Don't forget to close it when done. Still better, use the with statement to make sure that the file is automatically closed when its block is done.


In [ ]: