Pyhton call up all lines in text-file | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren

Pyhton call up all lines in text-file

I created a text-file. My first line is: > 1 Start Writing something: After adding a line with: file = open("first-file.txt", "w") file.write("I am the second line.") file.close() file = open("first-file.txt", "r") print( > I am the second line. ! PROBLEM FOR ME: Python only prints out the line I added, but not my first Line with the text "Start Writing something" ? MY QUESTION: ????? Why is it like that? ????? How can I make python printing the whole text-file? Thank You for Your Help. It's much appreciated. Greetings from Susi

4th May 2020, 9:14 AM
Susi - avatar
2 Antworten
+ 4
writing to a file that already have a content you will need to append to it and not just write, because 'w' erase everything and put in your new sentence
4th May 2020, 9:26 AM
Ôť│AsterisKÔť│ - avatar
+ 4
It is the whole text-file - unfortunately. 'w' indicates that the file will be *overwritten*. If you don't want that, you need to use another file mode. 'a' comes to mind.
4th May 2020, 9:28 AM
HonFu - avatar