[Tutor] Retrieving information from a plain text file (WinXP/py2.6.2/Beginner)
Serdar Tumgoren
zstumgoren at gmail.com
Mon Nov 2 20:43:43 CET 2009
Hi Katt,
It appears you did not return the list of reminders that you extracted
in the "read_reminders" function, but simply printed them from inside
that function.
If you modify your code as below to store the list in a variable
called "reminders", you should be able to access the list in your
global namespace.
> def read_reminders():
> print "\nReading text file into program: reminders.txt"
> text_file = open("reminders.txt","r")
> reminders = [line.strip().split("'") for line in text_file]
> text_file.close()
> print reminders
return reminders
Also, on a side note, you can greatly improve the readability of your
code by using the triple-quote style for multi-line docstrings inside
functions (rather than the hash comment marks). I tend to use hash
marks for one-line/inline comments, since they can really become an
eyesore (at least IMHO) when used too liberally.
Also, Python's whitespace and code formatting conventions can handle a
lot of the "documentation" for you. For instance, module imports are
typically always performed at the top of a script, so it's reasonable
to expect that others reading your code will understand you're
importing some modules.
Much of this spelled out in PEP's 8 (style guide) and 257 (doc strings):
http://www.python.org/dev/peps/pep-0008/
http://www.python.org/dev/peps/pep-0257/
HTH!
Serdar
More information about the Tutor
mailing list