Ich versuche mich gerade an meinem ersten Programmchen um Python zu erlernen.
Leider komme ich nicht wirklich weiter im Moment und deshalb wende ich mich mal an euch.
Kurze beschreibung was das Programm machen soll
- Configfile einlesen.
name1 pfad1 pfad2 <= das ist eine Zeile des configfiles
- Beim einlesen des configfiles wird für jeden namen eine instance der klasse Programm erzeugt und zusätzlich der name in die Liste Programmliste geschrieben.
- Nun arbeiten mit der Programm-klasse
- Jetzt config file wieder schreiben. <= hier ist das problem
-----------------------------------------------------
Mir ist klar das in diesem fall das eingelesen und das geschrieben file gleich sind da nichts gemacht wird zwischendurch.
Aber das ist auch nicht das Problem, sondern das ich es nicht schaffe das configfile richtig zu schreiben.
Ich bin für jede anregung dankbar!
--------------------------------------------------------
Nun zum code
Code: Alles auswählen
#!/usr/bin/python
Programmliste = []
import os
os.system('clear')
class Programm:
def __init__(self, name, pfad1, pfad2):
self.status = 0
self.name = name
self.pfad1 = pfad1
self.pfad2 = pfad2
print '(%s wurde initialisiert)' % self.name
def startProgramm(self):
if self.status == 0:
if os.path.isfile(self.pfad1):
print '%s wird gestartet...' % self.pfad1
self.status = 1
elif os.path.isfile(self.pfad2):
print '%s wird gestartet...' % self.pfad2
self.status = 1
else:
print 'konnte %s nicht starten da nicht vorhanden!' % self.name
self.status = 0
else:
print '%s laeuft bereits' % self.name
def stopProgramm(self):
if self.status == 1:
self.status = 0
print '%s wird gestopt...' % self.name
else:
print '%s ist bereits gestopt' % self.name
def programmStatus(self):
if self.status == 1:
print 'Status von %s ist ON' % self.name
else:
print 'Status von %s ist OFF' % self.name
def programmReturn(self):
print '%s %s %s %s' % (self.name, self.pfad1, self.pfad2, self.status)
#----------------------------------------------------------------------------
class Configprogramm:
def __init__(self):
pass
def saveConfig(self):
print '(Schreibe configfile %s ...)'
f = file('./das_configfile.conf.neu', 'w')
for i in Programmliste:
print i
conftext = i.programmReturn()
print conftext
f.write(conftext)
f.close()
def readConfig(self):
print '(Lese configfile...)'
f = file('./das_configfile.conf', 'r')
conffile = f.readlines()
f.close()
for i in conffile:
split = i.strip().split(' ')
# print 'split 0 ist %s' % split[0]
# print 'split 1 ist %s' % split[1]
# print 'split 2 ist %s' % split[2]
split[0] = Programm(split[0], split[1], split[2])
print '%s = Programm(%s, %s, %s)' % (split[0], split[0], split[1], split[2])
Programmliste.append(split[0])
f.close()
print '===> Nun ausgabe der confdate...'
a = Configprogramm()
print '---------------------- LESEN -------------------------'
a.readConfig()
print '-------------------- SCHREIBEN -----------------------'
a.saveConfig()