[Tutor] writing files using modules and functions

Alan Gauld alan.gauld at btinternet.com
Tue Nov 13 09:35:03 CET 2012


On 13/11/12 06:43, Rufino Beniga wrote:
> def MatInv(arr,file):
>      f = open('file.txt','w')
>      f.write(arr)
>      f.close()
>
> So I'm trying to write a module that will take a matrix (arr) and write
> it to a text file.
> The above module is called MatrixIO.py
>
> #first I import it
>
> import MatrixIO
>
> #Then i call the function MatInv with matrix(a) and file name (doc) as
> the arguments
> MatInv(a,doc)
>
> It creates a file called txt and it shows only boxes. What am I doing wrong?

We can't tell until we see your code. Assuming its not more than 100 
lines or so why not post it? If it is more then try creating a smaller 
example...

The most likely thing is that the data is binary and when you display it 
the values do not map to printable values. You probably need to convert 
the array to strings before writing. But that's just a guess because I 
can't see what you are doing.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list