title: Mad Libs ATBS with Python using regular expressions
body: <p>I am working on the &quot;Mad Libs&quot; project in ATBS. The program should read in text files and allow the user to add their own text anywhere 'ADJECTIVE', 'NOUN', 'ADVERB' or 'VERB' appears in the text file.</p>
  <p>The code I have so far is as follows:</p>
  <pre><code>    import os, re
  
      NewFile=input('Please enter the path of the file to be used:')
      NewFilepath=os.path.abspath(NewFile)
      print(NewFilepath)
      if os.path.exists(NewFilepath)==True: 
          open(NewFile, 'a')
      else: 
          print('This file does not exist. Creating a new file.')
          open(NewFile, 'w')
  
      NewFileContent=NewFile.read()
  
      def NewMadLibs(): 
          SpeechParts=re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB', re.I)
          match=re.findall(NewFileContent)
          for match in NewFileContent: 
              if match=='ADJECTIVE': 
                  adjective=input('Enter an adjective:\n')
              SpeechParts.sub(adjective, 'ADJECTIVE')
              if match=='NOUN': 
                  noun=input('Enter a noun:\n')
              SpeechParts.sub(noun, 'NOUN')
              if match=='ADVERB': 
                  adverb=input('Enter an adverb:\n')
              SpeechParts.sub(adverb, 'ADVERB')
              if match=='VERB': 
                  verb=input('Enter a verb:\n')
              SpeechParts.sub(verb, 'VERB')
  
      NewMadLibs(NewFileContent)
      UserMadLibsFile=NewFileContent.read()
      print(UserMadLibsFile)
      new_FileName='new_'+'source.txt'
      new_file=open(new_FileName, 'w')
      new_file.write(UserMadLibsFile)
      new_file.close()
      print(&quot;The file is saved as 'new_source.txt'&quot;)
  </code></pre>
  <p>The error message that appears looks like this:</p>
  <pre><code>    Traceback (most recent call last):
      File &quot;/Users/myname/Documents/MadLibsNewTry.py&quot;, line 16, in &lt;module&gt;
      NewFileContent=NewFile.read()
      AttributeError: 'str' object has no attribute 'read'
  </code></pre>
  <p>I'm confused by this error because I'm opening up the NewFile in either append mode or write mode depending on if the file (path) already exists. Shouldn't that  create or open &quot;NewFile&quot; and it would be a file object, not a string? The code didn't get to this point, but I figured that a similar error would occur for the line: UserMadLibsFile=NewFileContent.read() in the final block.</p>
  <p>I'm open to any suggestions or other methods that I should consider for this practice problem.</p>