[Tutor] writing files using modules and functions

Dave Angel d at davea.name
Tue Nov 13 10:08:28 CET 2012


On 11/13/2012 01:43 AM, 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?
>
>

Before you try to figure out how it'll work in two modules, make it work
in one script.  Since you don't post enough code to actually run it,
we'd only be guessing why.  And even the code you show is wrong.  The
call to MatInv won't work with that import statement;  you'd need to
qualify it.

def matrix(values):
     return something??

import Matrix10
a = matrix(42)
doc = something else, perhaps a string literal
Matrix10.MatInv(a.doc)

So clearly you have some different source,  if it ever gets as far as
writing to the file.

Strip the code to a minimal test (pref. under 50 lines)
Specify Python version, OS type and version
Specify website and version for any non-standard library you import 
(eg. perhaps matrix)
Show the filenames and contents for all the source you supply
Show the full traceback of any error you get,   OR
Explain what you expected it to do, and how it was different

If I had to make a wild guess, I'd say that matrix was some form of
collection of floating point numbers.  But I have no idea what it
supplies to the write method, nor why one would expect that it should be
printable.  Might help to look at the docs for matrix.



-- 

DaveA



More information about the Tutor mailing list