Python Forum
I don't understand the error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't understand the error
#1
I'm a Python 2 user and made a code to filter out the vowels in a phrase. This is the code:
filtered_phrase=open("vowel_filter.txt", "w")
filtered_phrase_2=open("vowel_filter.txt", "r")
vowel_filter=raw_input("Enter text: ")
for char in vowel_filter:
  if char=="a" or char=="A" or char=="e" or char=="E" or char=="i" or char=="I" or char=="o" or char=="O" or char=="u" or char=="U":
    pass
  else:
    filtered_phrase.write(char)
new_phrase=filtered_phrase_2.read()
print new_phrase
filtered_phrase.close()
filtered_phrase_2.close()
The weird part is, when I open vowel_filter.txt (the file that contains the altered phrase) the expected phrase is there, but when I attempt to read and print the file, it won't print anything. (I indented correctly the post just doesn't save the format)
Reply
#2
You have to close the file first and then open it again to read.

Try this:
filtered_phrase = open("file1.txt", "w")
vowel_filter = raw_input("Enter text: ")
for char in vowel_filter:
    if char == "a" or char == "A" or char == "e" or char == "E" or char == "i" or char == "I" or char == "o" or char == "O" or char == "u" or char == "U":
        pass
    else:
        filtered_phrase.write(char)
filtered_phrase.close()
filtered_phrase_2 = open("file1.txt", "r")
new_phrase = filtered_phrase_2.read()
filtered_phrase_2.close()
print new_phrase
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error I don't understand finndude 2 5,011 Oct-12-2022, 02:43 PM
Last Post: finndude
  Don't understand error message Milfredo 2 1,995 Aug-24-2020, 05:00 PM
Last Post: Milfredo
Exclamation Get syntax error. Dont understand why beLIEve 21 14,459 Aug-25-2019, 02:28 PM
Last Post: ThomasL
  Syntax error for a reason I don't understand DanielCook 2 2,408 Aug-07-2019, 11:49 AM
Last Post: buran
  Error in script don't understand, please help! DanielR 0 2,026 Mar-15-2019, 12:39 PM
Last Post: DanielR
  I don't understand where is the error ghiso983 3 2,430 Jan-29-2019, 02:46 PM
Last Post: perfringo
  Index Error, can't understand why. Diamond 5 4,176 Jan-03-2018, 11:45 AM
Last Post: Diamond

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020