Zeile in Textdatei löschen
Mit
?
Code: Alles auswählen
readline()[1:]
- __blackjack__
- User
- Beiträge: 14303
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
@egon11: Ich würde die erste Zeile beim einlesen schon überspringen.
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
Es würde dann bestimmt so aussehen:
Code: Alles auswählen
a = open("/meinedatei.txt")
file = a.readlines()[2:]
a.close()
b = open ("/meinedatei.txt", "w")
b.write(file)
b.close()
- __blackjack__
- User
- Beiträge: 14303
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Nein, weil das a) die ersten *zwei* Zeilen entfernt und b) trotzdem erst einmal alle Zeilen einliest.
Die Namen sind schlecht. `a` und `b` müssen nicht verschieden sein, es wäre aber besser wenn die tatsächlich beschreiben würden was der Name bedeutet. `file` ist keine Datei sondern eine Liste mit Zeilen.
Die Namen sind schlecht. `a` und `b` müssen nicht verschieden sein, es wäre aber besser wenn die tatsächlich beschreiben würden was der Name bedeutet. `file` ist keine Datei sondern eine Liste mit Zeilen.
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
- __blackjack__
- User
- Beiträge: 14303
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Oder man nimmt die Methode die tatsächlich mehrere Zeilen aus einem iterierbaren Objekt speichern kann.
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
So das ganze hab ich jetzt so gelöst:
Und das klappt so wie ich es haben wollte.
Code: Alles auswählen
allfile = open("/home/versuch.txt", "r")
fobj = allfile.readlines()[1:]
allfile.close()
for i in fobj:
print(i)
newfile = open("/home/versuch1.txt", "a")
newfile.write(i)
newfile.close()
Ist ist ziemliche Verschwendung, die Datei für jede Zeile nochmal neu zu öffnen. fobj ist kein File-Objekt sondern eine Liste mit Strings. i ist kein Index sondern ein Zeile.
Code: Alles auswählen
with open("/home/versuch.txt") as lines:
with open("/home/versuch1.txt", "w") as output:
_ = next(lines) # skip first line
output.writelines(lines)- __blackjack__
- User
- Beiträge: 14303
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
@egon11: Glaube ich nicht. Jedenfalls nicht wenn man das mehr als einmal laufen lässt oder `/home/versuch1.txt` aus einem anderen Grund vorher schon existiert.
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
