-1

I'm running a python code, The code is:

f = open("File.txt", "r")
f.write("my first file\n")
f.write("my second file \n\n")
f.write("contains three lines\n")
f.read(2)

but I'm keeping have an error:

Try the new cross-platform PowerShell https://aka.ms/pscore6
PS F:\ping> python -u "f:\ping\file.py"
Traceback (most recent call last):  File "f:\ping\file.py", line 2, in <module>
f.write("my first file\n")
io.UnsupportedOperation: not writable
 

What is wrong with my code?

0

1 Answer 1

0

You open the file in read mode. Change the "r" to "w".

f = open("File.txt", "w")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.