python: several functions in one file, AttributeError 'module' object has no attribute
Question: I am a beginner of python, and I just created a module file in python which
includes several functions together. When I called the first function defined
in the file, it was fine. But when I tried to call the second function it
says:

`AttributeError: 'module' object has no attribute 'file2file'` (file2file is a
function I defined by myself)

Here is the code of the file

    
    
    import sys
    import scipy as sci
    import scipy.sparse as sp
    import numpy as np
    
    def file2map(inf):
        dic = dict()
        with open(inf, "r") as fin:
            for line in fin:
                s = line.split("\t")
                dic[(int(s[0]),int(s[1]))] = float(s[2])
        return dic
    
    def file2file(inf,outf):
        with open(inf, "r") as fin:
            with open(outf, "w") as fout:
            for line in fin:
                s = line.split("\t")
                fout.write("t{0}\t{1}\t{2}\n",s[0],s[1],s[2])
    

the file's name is `dataprocessing.py`, when I typed
`dataprocessing.file2map('xxx.data')`, it is fine, but generated an error
message of `AttributeError: 'module' object has no attribute 'file2file'` when
I typed `dataprocessing.file2file('xxx.data','out.data')`.

Thank you very much!


Answer: Maybe this is not the thing that causes your error, but I see that you missed
an indent in the most inner for loop in `file2file(inf,outf)`

It should be:

    
    
    def file2file(inf,outf):
        with open(inf, "r") as fin:
            with open(outf, "w") as fout:
                for line in fin:
                    s = line.split("\t")
                    fout.write("t{0}\t{1}\t{2}\n",s[0],s[1],s[2])
    

