How to prevent print from overwriting a text file? [duplicate] - python

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 8 years ago.
This code actually overwrite the pre-existing content of out.txt, is there a way to make it print in the next line ?
with open(r'C:\out.txt', "w") as presentList:
print("Hello", file=presentList)

Use "a" instead of "w".
This appends new text at the end.

I think you'll want to open with "r+" instead (opening with "w" overwrites the file!). If you aren't partial to using with, you can do
f = open("C:/out.txt","r+")
f.readlines()
f.write("This is a test\n")
f.close()
The f.readlines() will ensure that you write to the end of the file instead of overwriting the first line if you need to write more. As the other person said, you can also open with "a" too

Related

Why do i get an empty file when trying to edit it using pyhon? [duplicate]

This question already has answers here:
Difference between modes a, a+, w, w+, and r+ in built-in open function?
(9 answers)
Closed 1 year ago.
I am trying to edit a text file that is html using python. When printing it, it gives an empty file. Why it gets empty? I tried to print it because I don't know how to return it.
Here's the code:
import bleach
with open ('index1.txt','w') as f: #to open the file that contains html markups
bleach.clean(
'f',
tags=['p'],
attributes=['style'],
styles=['color'],
)
f=open('index1.txt')
content = f.read()
f.close()
print(content)
It becomes empty because you open file for writing with 'w' and thus make it empty as per documentation - just change it to 'r' or 'a'
It would remain empty because you're just creating a file & not writing anything on it.

Can't read a file with w+ [duplicate]

This question already has answers here:
Difference between modes a, a+, w, w+, and r+ in built-in open function?
(9 answers)
Closed 1 year ago.
heres the code
doc = open(path, 'w+')
lines = []
i = 0
for line in doc:
lines.append(line)
i += 1
if i == 1:
money = int(line)
print(lines)
heres what is returns
[]
the text file contains one line that says "1000"
I tried to resolve the issue by opening it with r, close it, and open it again with w. But for some reason it just deletes all of the text in the file when I try to write anything.
opening it with w+ will overwrite the existing file with a new empty file (which you can then both write and read from) ...
you probably want to open it with a+ (to append) this will put the pointer at the end of the file and then to read before that you will need to use open_file.seek(0) (to go to the beginning of the file)

Using 'w' and 'r' in txt files [duplicate]

This question already has answers here:
How to open a file for both reading and writing?
(4 answers)
Closed 2 years ago.
How can I use the 'r' and 'w' at the same time?
Hi I want to know if I can use 'r' and 'w' at the same time, the following is an example
# Example
with open('txt.txt', 'w') as t:
t.write('Hello World')
t.readable()
eg = t.readlines()
When I write this down it will create the txt file but it gives me an error.
I Want to know how to use 'r' and 'w' at the same time
Please help me
I am suffering
I need help
You use r+. The r character means read-only, w means write-only, r+ means read and write.
Just keep in mind that you'll probably need to manage the file pointers (where your current position is) when switching between read and write operations.

Why isn’t there a output when i try to print the list of lines from a file? [duplicate]

This question already has answers here:
iterating over file object in Python does not work, but readlines() does but is inefficient
(3 answers)
Closed 3 years ago.
I was doing an exercise with the help of tutorial of python, trying to combine the codes used to read contents of file line by line and to make a list of lines from a file. The output I expected is that the text will be printed 2 times, which doesn’t work.
My operating system is Windows 10.
I use sublime text 3.
with open(filename) as f:
for line in f:
print(line)
lines = f.readlines()
for line in lines:
print(line.strip())
Output:
In Python you can use list.
In Python you can use class.
In Python you can use dictionary.
[Finished in 0.1s]
Why doesn’t print(line.strip()) work?
The reason is f is a file iterator which is completely exhausted after the loop (it is one-time consumable). There is nothing in f to read from and hence f.readlines() returns an empty list.
In fact, if you want to iterate over file lines over again, you should use f.readlines() to store list and iterate over that list any number of times.

write function parameter inside file - Python [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 5 years ago.
I have a function which prints messages. then I want to save this messages into a file. But when I use .write(function parameter) it only write last message inside my file
writing_in_log = True
def print_and_log(message):
if write_in_log is True:
logFile = open("log", "w+")
logFile.write(message)
logFile.close()
I suppose you are not using the 'a' parameter when opening the file:
with open('file.txt', 'a') as file:
file.write('function parameter')
You probably open the file for each writing with open(yourfile, 'w') which will erase any content from the file before you write to it. If you want to append to your file, use open(yourfile, 'a').
In case this is not the error we need more information about what you are doing, i.e. the relevant parts of the code.

Categories