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

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.

Related

How can I open file on Mac? Python [duplicate]

This question already has an answer here:
Why can't I write to a file that I open in python?
(1 answer)
Closed 9 months ago.
I try to open and write something in file, but it gives that mistake:
(me Operation not permitted: '/Users/archi/Desktop/Python/txt.pages)
I have tried to give permission to file, but all that doesn't work.
here is the code
r = open('/Users/archi/Desktop/Python/txt.pages')
r.write('something')
r.close
`
try this:
with open('/Users/archi/Desktop/Python/txt.pages', 'w') as r:
r.write('something')
although i'm not sure how much help it's going to be as you are opening a .pages file which is encoded differently than a standard text file -- it contains a compression with styling information, text, etc.
Are you missing the second parameter for .open()? The default is 'r' which does not allow you to write to the file, in my understanding.
From https://docs.python.org/3/library/functions.html#open:
The default mode is 'r' (open for reading text, a synonym of 'rt'). Modes 'w+' and 'w+b' open and truncate the file. Modes 'r+' and 'r+b' open the file with no truncation.

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.

What is the exact difference between r+ and w+ modes? [duplicate]

This question already has answers here:
Difference between r+ and w+ in fopen()
(7 answers)
Closed 6 years ago.
I tried to use both of them opening a file but got the same result.
w+ is supposed to create a new file if it doesn't exist while r+ not.
Is this the only difference?
For example, this is an initial file file.txt
Line 1
Line 2
Line 3
1) file.txt after implementing of r+ mode (writing the word "hello")
hello
2) file.txt after implementing of w+ mode
hello
I guessed that r+ would append an input to the beginning of the file.
EDITED:
So, the 1st difference is about creating a file that doesn't exist and the 2nd difference is that r+ overwrites while w+ truncates, right? (than the question arises whether overwriting and truncating are the same concepts or not)
Remove file.txt first and you'll understand the difference.
w+ will create the file.

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

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

Python: Saving a string to file without overwriting file's contents [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 8 years ago.
I need to add my program's outputs to a saved file every time I run the program without overwriting the previous outputs.
This is how I'm writing to a file now:
lead.write(str(alldates))
Any ideas?
Here, you should open the file in append mode:
append_text = str(alldates)
with open('my_file.txt', 'a') as lead:
lead.write(append_text)
There are lots of specialised ways to open files for different purposes, and you can read about them in the documentation.
You should open the file in append mode.

Categories