-1
import os, sys
pandaFile = open("panda.txt", "w")
pandaRead = pandaFile.read()

if "NOON" in pandaRead:
    print("Enter a noon:")
    noon = input()
    str.replace("NOON",noon)

if "ADJECTIVE" in pandaRead:
    print("Enter an adjective:")
    adjective = input()
    str.replace("ADJECTIVE", adjective)

if "VERB" in pandaRead:
    print("Enter and verb:")
    verb = input()
    str.replace("VERB",verb)

newcontent = open("panda.txt","w")

My goal is to open the file "Panda.txt" file. If there is ADVERB, VERB, or NOON, replace these string by the user input.And re-write the "panda.txt" file. My error code is: pandaRead = pandaFile.read() io.UnsupportedOperation: not readable I am using Sublime text 2 with Python 3.4 on a Windows Vista Home Version.

aveuiller
  • 1,511
  • 1
  • 10
  • 25
Yann H.
  • 23
  • 3

1 Answers1

2

There was several errors within your code. I will explain them to you, but for further development, please always keep the Python reference website to see the usage of the methods you call.

Here are the various errors I had to deal with from your code:

Taking these into account, the following code sample is (I assume) what you intend to do:

import os , sys

# With is a special bloc statement,
# closing your variable pandaFile at the end of the bloc.
with open("panda.txt", "r") as pandaFile:
    pandaRead = pandaFile.read()

if "NOON" in pandaRead:
    noon = input("Enter a noon:")
    pandaRead = pandaRead.replace("NOON", noon) 

if "ADJECTIVE" in pandaRead:
    adjective = input("Enter an adjective:")
    pandaRead = pandaRead.replace("ADJECTIVE", adjective)

if "VERB" in pandaRead:
    verb = input("Enter and verb:")
    pandaRead = pandaRead.replace("VERB",verb)

with open("panda.txt", "w") as pandaFile:
    pandaFile.write(pandaRead)
aveuiller
  • 1,511
  • 1
  • 10
  • 25
  • Thanks Antoine. Clean code! Now I am getting the error message adjective = input("Enter an adjective:") EOFError: EOF when reading a line . I was thinkng maybe it's because I am using Sublime text. And it doesn't really allow input().But the first input for the noon works. – Yann H. Aug 30 '15 at 17:50
  • Sorry but I can't reproduce your errors, i am working on linux though. How do you run your script and what do you try to input? – aveuiller Aug 30 '15 at 18:24
  • That's alright. Thanks a lot though. I run the script from the command line.I have more than 1 NOON , maybe that's why? – Yann H. Aug 30 '15 at 18:35
  • I guess this is the command line from sublime text since your problem is quite similar to this one: http://stackoverflow.com/questions/12547683/python-3-eof-when-reading-a-line-sublime-text-2-is-angry – aveuiller Aug 30 '15 at 18:49