-2

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
lll
  • 92
  • 8
  • to add to the file and not delete: `f=open("guru99.txt", "a")` to read and write: `f=open("guru99.txt", "r+")` to write: `f=open("guru99.txt", "w+")` – L-S Feb 13 '21 at 01:07

1 Answers1

3

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953