Seite 1 von 1

[Gelöst] Wie vom ``file``-Objekt ableiten?

Verfasst: Sonntag 28. Januar 2007, 17:12
von sape
Hi.

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os

class PseudoFileOut(file):
    def __new__(cls, path):
        return file.__new__(cls, path, 'w') 
        
path = os.path.join(sys.path[0], 'test.txt')
stream = PseudoFileOut(path)
stream.write("test2")
stream.close()

Code: Alles auswählen

IOError: [Errno 0] Error
lg

Verfasst: Sonntag 28. Januar 2007, 17:39
von BlackJack
So sollte es funktionieren:

Code: Alles auswählen

class PseudoFileOut(file):
     def __init__(self, path):
         file.__init__(self, path, 'w')

Verfasst: Sonntag 28. Januar 2007, 17:45
von sape
Danke. Und ich dachte schon mit ``__new__`` wegen imutable.

lg