Pyhton call up all lines in text-file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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(file.read()) > 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
Susi - avatar
2 Answers
+ 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 https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2447/
4th May 2020, 9:26 AM
✳AsterisK✳
✳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. https://www.geeksforgeeks.org/file-handling-JUMP_LINK__&&__python__&&__JUMP_LINK/
4th May 2020, 9:28 AM
HonFu
HonFu - avatar