-4

So I made a script like this:

a = open("movies.txt","r")

And I wanted to write something in this so I did:

a.write("EndGame")

But it said it could not write, why?

Red
  • 26,798
  • 7
  • 36
  • 58
  • Because you opened the file for reading only. – mkrieger1 Jul 14 '20 at 19:49
  • You used the read handler, `"r"`. Instead, use `a = open("movies.txt","w")`. Check out [this link](https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files). – A.J. Uppal Jul 14 '20 at 19:49
  • You're mixing up the [three Rs](https://en.wikipedia.org/wiki/The_three_Rs). – Peter Wood Jul 14 '20 at 19:50
  • Welcome to Stack OverFlow! Your question currently a [Duplicate](https://stackoverflow.com/help/duplicates), which doesn't meet this site's requirements. Please edit your question following these guidelines: https://stackoverflow.com/help/how-to-ask – Red Jul 14 '20 at 19:51

1 Answers1

0

that is because you have entered a wrong file mode. You used r which stands for reading while you should use the w for the writing. That code should work.

a = open("movies.txt","rw")
a.write("EndGame")
a.close()
Matthias
  • 12,873
  • 6
  • 42
  • 48
Aleksander Ikleiw
  • 2,549
  • 1
  • 8
  • 26