ich versuche gerade, mich in Objektorientierung einzuarbeiten, und komme an einer Stelle nicht weiter. Vielleicht kann jemand von euch mir helfen?
Zum Üben habe ich ein kurzes Programm geschrieben, dass .tmp-Dateien und .tmp-Verzeichnisse löschen soll. Beim Löschen soll sowohl die Datei (bzw. das Verzeichnis) gelöscht werden, als auch das dazugehörende Objekt. Das scheint mit unten stehendem Programm auch zu funktionieren.
Code: Alles auswählen
#!/usr/bin/env python2
import logging, os
class path(object):
count = 0
def __init__(self, abspath):
logging.debug('function: path.__init__(\'' + abspath + '\')')
self.abspath = abspath
if os.path.isfile(abspath):
self.__type = 'file'
elif os.path.isdir(abspath):
self.__type = 'dir'
else:
self.__type = 'other'
path.count +=1
def __del__(self):
logging.debug('function: path.__del__(\'' + self.abspath + '\')')
if self.__type == 'file':
os.remove(self.abspath)
logging.debug(' os.remove(\'' + self.abspath + '\')')
if self.__type == 'dir':
os.rmdir(self.abspath)
logging.debug(' os.rmdir(\'' + self.abspath + '\')')
path.count -=1
def __str__(self):
logging.debug('function: path.__str__(\'' + self.abspath + '\')')
return '(\'' + self.abspath + '\', \'' + self.__type + '\')'
def settype(self, newtype):
logging.debug('function: path.settype(\'' + self.abspath + '\': ' + newtype + ')')
self.__type = newtype
# main
# create test scenario: empty file '/tmp/1.tmp' + empty dir '/tmp/2.tmp':
os.system('rm -rf /tmp/1.tmp && rm -rf /tmp/2.tmp && touch /tmp/1.tmp && mkdir /tmp/2.tmp && sync')
# define logging; possible values: CRITICAL, WARNING, INFO, DEBUG, NOTSET:
logging.basicConfig(format = "%(asctime)s: %(message)s", datefmt = "%Y-%m-%d %H:%M:%S", level=logging.DEBUG)
pathnamelist = ['/tmp/1.tmp', '/tmp/2.tmp'] # list of pathes to delete (files: always; dirs: if empty)
# build 'pathes' (= list of path objects):
pathes = []
for pathname in pathnamelist:
pathes.append(path(pathname))
# print pathes:
#for onepath in pathes:
# print str(onepath)
# delete all files and dirs in pathes
while len(pathes) > 0:
logging.info('------------------------------------------------------------------------')
logging.info('length of object list "pathes": ' + str(len(pathes)) + ', number of "path" objects: ' + str(path.count))
del pathes[0]
logging.info('length of object list "pathes": ' + str(len(pathes)) + ', number of "path" objects: ' + str(path.count))
1.: Die letzte mit "logging" ausgegebene Zeile lautet jetzt nicht mehr
, sondern2017-11-25 18:35:01: length of object list "pathes": 0, number of "path" objects: 0
2.: Nach dieser Zeile kommt eine weitere Meldung, die ich nicht verstehe:2017-11-25 18:35:01: length of object list "pathes": 0, number of "path" objects: 1
Exception AttributeError: "'NoneType' object has no attribute 'debug'" in <bound method path.__del__ of <__main__.path object at 0x7ff9ddb23b10>> ignored
Meine Fragen:
Warum werden beim Programmlauf ohne "print str(onepath)" alle Objekte der Klasse "path" gelöscht, während beim Programmlauf mit der "print str(onepath)"-Schleife ein Objekt übrig bleibt?
Und: Worauf bezieht sich der "Exception AttributeError"?
Vielen Dank für eure Hilfe!