How to write to a file without overwriting current contents? [duplicate] - python

This question already has answers here:
How do I append to a file?
(13 answers)
Open a text file without clearing everything in it?
(4 answers)
Closed 8 years ago.
with open("games.txt", "w") as text_file:
print(driver.current_url)
text_file.write(driver.current_url + "\n")
I'm using this code right now, but when it writes to the file it overwrites the old content. How can I simply add to it without erasing the content that's already there.

Instead of "w" use "a" (append) mode with open function:
with open("games.txt", "a") as text_file:

Related

Cant write in a file to save a score on a game [duplicate]

This question already has answers here:
.write not working in Python
(5 answers)
Closed 3 years ago.
I am using:
text_file = open('files/highscoresnake.txt', 'w')
word = str(score)
text_file.write(word)
to save a highscore on a game.
however this does nothing to the text file
anyone know why?
You need to close the file when you're done. Try text_file.close() afterward.
Better yet, use with:
with open('files/highscoresnake.txt', 'w') as file:
file.write(word)
with will close the file after you exit that block of code.

Write to the last line of a text file? [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 1 year ago.
I am trying to get the program to automatically start at the last line of a text file when I run it and make it write on the last line. My current code is as follows:
with open('textfile.txt', 'a+') as tf
last_line = tf.readlines()[-1]
tf.write(linetext + '\n')`
When I run this, it says that the list index is out of range. How do I get this to automatically skip to the last line of a text file and start writing from there?
Use the a flag while opening the file
with open('path/to/file', 'a') as outfile:
outfile.write("This is the new last line\n")

python readline() output nothing [duplicate]

This question already has answers here:
Confused by python file mode "w+" [duplicate]
(11 answers)
Closed 6 years ago.
I got nothing back from the command readline(). I am new to python and totally confused now.
my_file = open("test.txt", "w+")
my_file.write("This is a test")
print my_file.readline()
When you write to a file, you overwrite any previous contents of the file and leave the pointer at the end of the file. Any attempt to read after that will fail, since you're already at the end of the file.
To reset to the beginning of the file and read what you just wrote, use:
my_file.seek(0)
Because after you wrote content in you file. the cursor is at the end of the file. Before you use readline(), use my_file.seek(0) first, If your file content is only This is a test, you can get your want. Deep into this, please go to : https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files

File gets cleared when try to write to it [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 6 years ago.
I am try to make it possible to save progress in my python game put the following line is giving me problems:
dataW = open("data.dat","wb")
How can I stop this line from clearing my file.
You're opening the file in write mode:
'w' for only writing (an existing file with the same name will be
erased)
Emphasis mine
You meant to use the append mode:
'a' opens the file for appending

How do I overwrite the text in a file using python3 so that the user can write more with out it interfering with old text? [duplicate]

This question already has answers here:
How to empty a file using Python
(2 answers)
Closed 6 years ago.
I'm confused and don't know what to do about this. I'm trying to overwrite the files text.
when opening a file with 'w' flag, it will rewrite the file if it exists.
with open('yourfile.ext', 'wt') as fileObj:
fileObj.write(stuff)

Categories

Resources