title: Python: IOError: [Errno 2] No such file or directory: - But file exists
body: <p>I wrote a small script to generate a json dict and insert it into the end of a json file I'm using. I think there's something going on with the way I'm opening the json file and modifying it.</p>
  
  <p>Here's the jist:</p>
  
  <ol>
  <li>It generates a json dict from raw_input</li>
  <li>It opens the json file and reads the lines, minus the last two containing "} ]"</li>
  <li>It writes the file back without the last two lines.</li>
  <li>Then, it writes the new dict entry with the closing "} ]"</li>
  </ol>
  
  <p>The code:</p>
  
  <pre><code>JSON_PATH = "~/Desktop/python/custconn.json"
  
  def main():
      # CLI Input
      group_in = raw_input("Group: ")
      name_in = raw_input("Name: ")
      nick_in = raw_input("Nick: ")
      host_in = raw_input("Host: ")
      user_in = raw_input("User: ")
      sshport_in = raw_input("SSH Port: ")
      httpport_in = raw_input("HTTP Port: ")
  
      # New server to add
      jdict = {
          "group": group_in,
          "name": name_in,
          "nick": nick_in,
          "host": host_in,
          "user": user_in,
          "sshport": sshport_in,
          "httpport": httpport_in
      }
  
      # Remove trailing "} ]" in json file
      with open(JSON_PATH, mode='r') as wf:
          lines = wf.readlines()
          lines = lines[:-2]
          wf.close()
      # Write change
      with open(JSON_PATH, mode='w') as wf:
          wf.writelines([item for item in lines])
          wf.close()
      # Write new server entry at the end
      with open(JSON_PATH, mode='a') as nf:
          nf.write("  },\n")
          nf.write("  {}\n".format(json.dumps(jdict, indent=4)))
          nf.write("]\n")
          nf.close()
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./jbuild.py", line 47, in &lt;module&gt;
      main()
    File "./jbuild.py", line 30, in main
      with open(JSON_PATH, mode='w') as wf:
  IOError: [Errno 2] No such file or directory: '~/Desktop/python/custconn.json'
  </code></pre>
  
  <p>The file does exist at that path, however..</p>