Python: file-object conflicts
Question: I'm beginning with python and I created a class of file-object deriving from
the 'file' class to be able to manipulate large datafiles. I have created
specific methods to work on these files that are built like shown below. I
need to return a new instance of MyClass after each method to be able to keep
working on them. The problem is that when I use several time the same method
there are conflict with the file foo.txt that is used as a temporary file
because it is still used as the support of MyClass instance. Is there a way
around this ? Or do I need to create randomized names for all my files ?

Thanks for your answers, George

    
    
    Class MyClass(file):
        "Similar to open(file_path,'r')"
        def __init__(self,file_path):
            file.__init__(self,file_path)
        self.tell()
    
        def method(self):
            """Extract informations from self to write them in a temporary file foo.txt
            that will be used to return a new instance of MyClass"""
            output=open('foo.txt','w')
            self.seek(0)
            # Extracting information from self 
            output.write(information)
            output.close()
            return MyClass('foo.txt')
    

I have completed the exemple to make it more clear. I think that my problem is
that I am returning a new instance of MyClass from a file I create in the
method. If I use the method several times without closing the instances
generated from foo.txt, the instances become empty and return 'None' because
foo.txt is re-written. For exemple:

    
    
    a=MyClass('/path/to/file')
    b=a.method()
    c=b.method()
    

Then returns an error because b is empty


Answer: well, your diagnostic is right, and your problem is that your use case is
wrong. If you do want to do what it looks like you want, here's how it should
work:

    
    
    a=MyClass('/path/to/file')
    a.method('/path/to/foo.txt')
    b=MyClass('/path/to/foo.txt')
    b.method('/path/to/bar.txt')
    c=MyClass('/path/to/bar.txt')
    

which is simple and will work well (of course, I won't give the details of how
you give the argument to `open()`). Though, it lacks elegancy.

To improve on that, you may want to create unique temporary files, which will
enable you to do what you want:

    
    
    a=MyClass('/path/to/file')
    b=a.method()
    c=b.method()
    

but the problem is that you still don't handle correctly the newly
created/opened temporary files. So, you'd better use context manager:

    
    
    with open('/path/to/file') as a:
        with MyClass(a).method() as b:
            with MyClass(b).method() as c:
                # use a, b and c files here
                pass
    # and now, a is closed, b and c are deleted.
    

so you close the files when you don't need them anymore:

    
    
    import tempfile
    
    Class MyClass():
        def __init__(self,file):
            self.file = file
    
        def method(self):
            """Extract informations from self to write them in a temporary file foo.txt
            that will be used to return a new instance of MyClass"""
            output = tempfile.TemporaryFile()
            self.seek(0)
            # Extracting information from self 
            output.write(information)
            return output
    

of course, that's one way of doing it, you may work it out in many other ways.
The beauty of this solution is that your [temporary file gets deleted when you
`close()` is
called](http://docs.python.org/2/library/tempfile.html#tempfile.TemporaryFile),
which happens when you exit the context of the file.

Of course, there are other ways to implement this, but I find this is the most
simple and explicit way to work it out (given you give an explicit name to
`method()` of course).

HTH

