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

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

Related

How to change a file to be not executable through python? [duplicate]

This question already has answers here:
Changing file permission in Python
(9 answers)
Closed 5 years ago.
I need to change the permissions on a file so that it cannot be executable. I still need to be able to read it through open("filename", 'rb'). How can I do this in Python?
change it to a text file
a text file can't be executed but read

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

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)

How to create a file (a text file as default if available) in python code [duplicate]

This question already has answers here:
Create empty file using python [duplicate]
(2 answers)
Closed 8 years ago.
This is the code I have so far, yes I know the 'dirs' code creates a folder, but I am not sure of the code (if there is one) to create a file instead!
import os
if os.path.isfile(outfile):
print("Name already used, please choose another")
else:
os.makedirs(outfile)
Any help would be appreciated :D
Inorder to create a file and work on it, use the open() function.
fp = open(outfile, mode)
modes:
r: read
w: if file exists, replace it with a new one
a: append existing file
In your case the mode is 'w'
For create a file you can just open a file in write mode
open('file.txt', 'w')
https://docs.python.org/3/library/functions.html#open

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

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:

Categories

Resources