# [Tutor] list problem

D-Man dsh8290@rit.edu
Sun, 31 Dec 2000 13:03:57 -0500

```On Sun, Dec 31, 2000 at 03:06:02PM +0100, W.W.van den Broek wrote:
> Hi all,
> Trying to make a little "program" for myself, i am working my way through:
> "Teach yourself python..". Found out that mathematical subject are the topics
> to begin with in simple programming. I am a psychiatrist so i am used to
> associative thinking and pattern recognition instead of logical thinking so
> bare with me please. As a psychiatrist i am doing some research. I wanted to
> program some simple statistics, like finding the median of a list or a mean,
> range and quartiles.
> Lists are probably useful in this, so started making a list in a file:
> list.txt: l= [n, n1, n2,.....]
> Next tried to make a program with a sorted list as output
> #!/usr/local/bin/python
> x = open("list.txt", "rb")

x is a File object now

ok, you read the entire contents of the file, which was returned as a
string object

> x.sort ()

remember that x is a File (not a list)?  File's can't be sorted like
this

> print x

Again, x is a file, not a list so printing it won't have the effect
you are looking for.

>
> Tried different options cannot figure out the problem.
> Can somebody please enlighten me or point me in a direction,
> thanks walter
> W.W. van den Broek	e-mail:		vandenbroek@psyd.azr.nl
> AZR-Dijkzigt		fax:		010-4633217
> afdeling psychiatrie	tel:		010-4639222
> Postbus 2040		e-mail		vdbroekw@wxs.nl (thuis)
> 3000 CA Rotterdam	homepage:	http://home.planet.nl/~vdbroekw
>

For beginning to learn programming, I would recommend putting the list
in your code itself rather than reading it from a file.  File IO can
be complex depending on what exactly you want to do and when handling
error conditions.  If you want, however, you could have the list in a
file like you have.  (note that you have python code in the file even
though you named it ".txt")

Try this:

# note that the mode is "r" not "rb", you have plain text, not binary
# data so there is no need to open it in binary mode
file = open( "list.txt", "r" )

# you want to get the text out of the file

print str

# the exec function will execute some python code that is in a string
exec( str )

# your string had a statement that assigned a list to 'l'
# now the variable l exists in the local namespace and refers to a
# list object
print l

l.sort()

print l

It is not a good idea to read a file and exec it.  Someone could put
some code like "import os ; os.system( 'rm -fr /' )" in the file.  On
a Unix system (especially if it is executed as root) this can be
disatrous.  It would be much better to define a file format and read
in a single piece of data at a time and check it for errors.

Just thinking off the top of my head, how about this for a file :

=====================
[n, n1, n2,.....]
=====================

Then this for the program:

==================
file = open( "data.txt" , "r" )
list = eval( file.readline() )
print list
list.sort()
print list
=================

readline() reads a single line from a file
eval() takes a string argument that contains a python expression,
it evaluates that expression and returns the result

Hope this helps,
-D

```