title: Python: User inputs filename to write/be written to. program then opens/reads
body: <p>I am very new to programming and I have tasked myself to write a useless program to aid in learning how to write some function code that didn't come out of a book. The purpose of the program is to write a file with a user inputted filename then add contents to the file. I've made it this far. The problem I am having is from the second half of the program. </p>
  
  <p>The second half is suppose to read you the contents of the file it just made. then ask you if you want to copy the contents to a new file. then assign the new file a user inputted name and copy the contents of the original file. </p>
  
  <p>I am having issues reading the old filename. and also  my output from the program looks like this:</p>
  
  <pre><code>Insert 'filename.txt' Here &gt;&gt;&gt; test.txt
  user input &gt;&gt;&gt; lolokay
  Traceback (most recent call last):
    File "testbed.py", line 45, in &lt;module&gt;
      main()
    File "testbed.py", line 43, in main
      copyToNew(newFile())
    File "testbed.py", line 23, in copyToNew
      oldFile = open(f"{f}", "r")
  OSError: [Errno 22] Invalid argument: "&lt;_io.TextIOWrapper name='test.txt' mode='w+' encoding='cp1252'&gt;"
  </code></pre>
  
  <p>full code below:</p>
  
  <pre><code># this program will open a file or make a new file and write to it.
  # it will then copy the file contents to a new file.
  
  def newFile():
      # opens user inputted filename ".txt" and (w+) makes new and writes
      f = open(input("Insert 'filename.txt' Here &gt;&gt;&gt; "), 'w+')
      # asks for user input to enter into the file
      usrInput = input("user input &gt;&gt;&gt; ")
      # writes user input to the file and adds new line
      f.write(usrInput)
      f.write("\n")
  
      # closes the file
      return f
      f.close()
  
  # copy contents and outputs to new file
  def copyToNew(f):
      oldFile = open(f"{f}", "r")
      fileContents = oldFile.read()
      print("\n",fileContents)
  
      # needs to asks user if they would like to copy file to new document
      print(f"Would you like to copy this (name{oldFile})? Y or N")
      usrInput = input("Y or N &gt;&gt;&gt; ")
      print(usrInput)
  
      if usrInput.lower() in {"y"}:
          print("Your file has been created.")
  
      elif usrInput.lower() in {"n"}:
          print("Goodbye.")
      else:
          copyToNew(f)
  
  # defines main
  def main():
      copyToNew(newFile())
  
  main()
  </code></pre>