python readline() output nothing [duplicate] - python

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

Related

How to fix "io.UnsupportedOperation: File not open for writing" Error? [duplicate]

This question already has answers here:
How to open a file for both reading and writing?
(4 answers)
Closed 3 years ago.
In simplest terms, when i try to run a short amount of code to delete the contents of a file and then rewrite stuff to that file, it pulls that error. I'm trying to get a temperature reading from a com port using the filewrite from CoolTerm, perhaps it's the fact that the file is being used by CoolTerm as well, so I can't edit it, but I'm unsure.
I've tried multiple ways to delete the file information e.g the file.close(), and others, but none seem to work.
while True:
file = open("test.txt","r")
file.truncate()
x = file.read()
x = x.split("\n")
print(x[0])
print(x[1])
time.sleep(3)
I expect the console to output the contents of file but it doesn't. Something that gives me a similar result of what i want would be the Console just outputting the last two entries of the file, rather than having to delete all of it than rewriting it.
Modified to r+ mode is ok, I have tested.
with open('./install_cmd', 'r+') as f:
print(f'truncate ago:{f.read()}')
f.truncate(0)
print(f'truncate after:{f.read()}')

How to avoid overwriting a text file? [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 4 years ago.
I use a click program the help of PyGame in Python. I want to use the program multiple times by opening up the program, and shutting it down and reopening it again.
So basically I open the program, click something, and it writes the click time into a .txt file. My problem is, when I shut down the program and run it again, it overwrites the .txt file. So my question is, how to avoid this?
#Pygame program....
f = open("test.txt","w")
f.write("write something")
Open the file in append mode
f = open('test.txt', 'a')

How do I add text to a file without deleting what is already there? [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 5 years ago.
I'm making a login system for my school task and when I try to add text to a Notepad file using Python it deletes what is already there so I can only store the information of one user at a time. Any idea how I can fix this? I've looked around on the internet but I can't find the right combination of words to get the search result I want... thanks in advance. I'm using IDLE (Python 3.2 GUI)
Edit: Problem solved, thanks for the help everyone.
You have to open file in append mode, for example:
f_obj = open('myfile','a+')
For that, you have to use "a" mode for appending data in a file.
f_obj = open("c:/file_path",'a')
f_obj.write("append")
or
with open("c:/file_path", "a") as f_obj:
f_obj.write("append")
You can use 'a for append' mode instead of 'r for right' mode, see following:
with open('your_exist_file.txt', 'a') as file:
file.write('appended_text')

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

Substitute file content in Python [duplicate]

This question already has answers here:
Confused by python file mode "w+" [duplicate]
(11 answers)
Closed 8 years ago.
I imagine this is a question asked already twenty thousand times, but I cannot understand why the file is always empty. I want to open a file, remove a string from the whole file and then rewrite the content, but the file ends up being empty. This is the code I use:
f = open(filename,'w+')
f.write(f.read().replace(str_to_del,""))
f.close()
But the file is always empty. If I instead use "r+" then the content is appended and I have a duplicate text in the file. I'm using Python 3.3 . What am I missing?
Opening the file in w+ mode truncates the file. So, your f.read() is guaranteed to return nothing.
You can do this by opening the file in r+ mode, reading it, then calling f.seek(0), then writing. Or by opening the file in r mode, reading it, closing it, reopening it in w mode, and writing. Or, better, by writing a temporary file and moving it over the original (which gives you "atomic" behavior—no possibility of ending up with a half-written file).

Categories

Resources