title: When using ftplib in Python 3.7, what is the proper way to detect if an ftp connection is closed or unopened?
body: <p>Based on the docs and all previous SO questions, the code here should work properly, but isn't. Couldn't find any duplicates. If there are, my bad.</p>
  
  <p>Basically, after calling <code>ftp.quit()</code> on a valid ftp connection, subsequent attempts to test whether it is connected fail. Running the try / except block below prior to <code>ftp.quit()</code> works properly ("Connection is open" is always printed).  </p>
  
  <p><strong>After closing the connection using ftp.quit():</strong>  </p>
  
  <p><strong>1.</strong> I tried catching errors when calling <code>ftp.voidcmd('NOOP')</code>.</p>
  
  <p>I get an <code>Attribute: NoneType...</code> error (which should be an indication that the ftp object reset to <em>None</em>). </p>
  
  <p><strong>2.</strong> But, checking <code>if ftp is None</code> also fails.  </p>
  
  <p><strong>3:</strong> And, just for additional confirmation, I check <code>type(ftp)</code>  </p>
  
  <p><strong>Code to reproduce: <em>(Result follows)</em></strong> </p>
  
  <pre><code># Connect
  import ftplib
  ftp = ftplib.FTP("FtpSite")
  ftp.login("user","passwd")
  
  # show success
  print(ftp.getwelcome())
  
  connected = True
  
  # Close connection
  ftp.quit()
  
  # Is ftp still a valid obj?
  print(type(ftp))
  
  # Is ftp None?
  if ftp is None:
      print("ftp is None.")
  else:
      print("ftp is still assigned, but closed")
  
  
  # check if open
  try:
      ftp.voidcmd("NOOP")
  except ftplib.all_errors as e:
      errorInfo = str(e).split(None, 1)[0]
      print(errorInfo)
      connected = False
  
  if connected:
      print("Connection is open.")
  else:
      print("Connection already closed.")
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>&lt;class 'ftplib.FTP'&gt;  # the output of print(type(ftp))
  
  ftp is still assigned, but closed  # the output of checking if None  
  
  Traceback (most recent call last):  # the output of calling ftp.voidcmd()
    File "./dailyfetch.py", line 27, in &lt;module&gt;
      ftp.voidcmd("NOOP")
    File "C:\python37\lib\ftplib.py", line 277, in voidcmd
      self.putcmd(cmd)
    File "C:\python37\lib\ftplib.py", line 199, in putcmd
      self.putline(line)
    File "C:\python37\lib\ftplib.py", line 194, in putline
      self.sock.sendall(line.encode(self.encoding))
  AttributeError: 'NoneType' object has no attribute 'sendall'
  </code></pre>
  
  <p>Related SO questions:
  <a href="https://stackoverflow.com/questions/15170503/checking-a-python-ftp-connection">Checking a Python FTP connection</a>
  <a href="https://stackoverflow.com/questions/23086383/how-to-test-nonetype-in-python">How to &quot;test&quot; NoneType in python?</a></p>
  
  <p>Docs:
  <a href="https://docs.python.org/3/library/ftplib.html" rel="nofollow noreferrer">https://docs.python.org/3/library/ftplib.html</a></p>