<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Retrbindary code not working ? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110334#M8572</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Use the FTP Directory to retrieve KMZ
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot read file "%s"' % FILE
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink(FILE)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '*** Downloaded "%s" to CWD' % FILE
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Do they seem right to you ? This is from the book Programming ArcGIS 10.1 with Python cookbook. When I ran it in Pyscripter 2.5.3.0 and it gave me the error :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\ArcpyBook\Appendix2\ftp.py", line 45, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 398, in retrbinary&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.voidcmd('TYPE I')&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 248, in voidcmd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.putcmd(cmd)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 178, in putcmd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.putline(line)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 173, in putline&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.sock.sendall(line)&lt;/P&gt;&lt;P&gt;AttributeError: 'NoneType' object has no attribute 'sendall'&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 06:37:23 GMT</pubDate>
    <dc:creator>DEAF_PROBERT_68</dc:creator>
    <dc:date>2021-12-11T06:37:23Z</dc:date>
    <item>
      <title>Retrbindary code not working ?</title>
      <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110334#M8572</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Use the FTP Directory to retrieve KMZ
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot read file "%s"' % FILE
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink(FILE)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '*** Downloaded "%s" to CWD' % FILE
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Do they seem right to you ? This is from the book Programming ArcGIS 10.1 with Python cookbook. When I ran it in Pyscripter 2.5.3.0 and it gave me the error :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\ArcpyBook\Appendix2\ftp.py", line 45, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 398, in retrbinary&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.voidcmd('TYPE I')&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 248, in voidcmd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.putcmd(cmd)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 178, in putcmd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.putline(line)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.1\lib\ftplib.py", line 173, in putline&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.sock.sendall(line)&lt;/P&gt;&lt;P&gt;AttributeError: 'NoneType' object has no attribute 'sendall'&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:37:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110334#M8572</guid>
      <dc:creator>DEAF_PROBERT_68</dc:creator>
      <dc:date>2021-12-11T06:37:23Z</dc:date>
    </item>
    <item>
      <title>Re: Retrbindary code not working ?</title>
      <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110335#M8573</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;makes no sense, since the whole script can't be seen in context, but whatever your self.sock object is, it is not the correct object type since it doesn't have an attribute called "sendall"&amp;nbsp; the object being returned is "None"&amp;nbsp; and its type is as follows&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = None
&amp;gt;&amp;gt; type(a)
&amp;lt;type 'NoneType'&amp;gt;
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:37:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110335#M8573</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T06:37:26Z</dc:date>
    </item>
    <item>
      <title>Re: Retrbindary code not working ?</title>
      <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110336#M8574</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import moduels
import ftplib
import os.path
from ftplib import FTP


HOST = 'ftp.nifc.gov'
DIRN = '/incident_Specific_Data/ALASKA/Fire_Perimeters/20090805_1500'
FILE = 'FirePerimeters_20090805_1500.kmz'


# Create a block to connection or not


try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot reach "%s"' % HOST
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print '*** Connected to host "%s"' % HOST


# to log in anonymously
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.login()
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot login anonymously'
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.quit()
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '*** Logged in as "anonymous"'


# To change to the directory in DIRN
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.cwd(DIRN)
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot CD to "%s"'&amp;nbsp; % DIRN
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.quit()
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '*** Changed to "%s" folder' % DIRN


# Use the FTP Directory to retrieve KMZ
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot read file "%s"' % FILE
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink(FILE)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '*** Downloaded "%s" to CWD' % FILE


# Disconnect from FTP
f.quit()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the full code&amp;nbsp; Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:37:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110336#M8574</guid>
      <dc:creator>DEAF_PROBERT_68</dc:creator>
      <dc:date>2021-12-11T06:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: Retrbindary code not working ?</title>
      <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110337#M8575</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The only thing remotely similar to 'sock' is 'socket' hmmm&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 17:55:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110337#M8575</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-03-15T17:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Retrbindary code not working ?</title>
      <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110338#M8576</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The issue with your script is that it keeps &lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.5;"&gt;processing after an error condition. &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;"&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;The sock object is invoked as soon as your create your FTP object (see the __init__ function in the &lt;/SPAN&gt;&lt;EM style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;ftplib.py&lt;/EM&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt; file). This means that the sock object is not a NoneType at this point.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.5;"&gt;However, as Dan mentions, the sock object is a NoneType when you attempt to execute the &lt;EM&gt;f.retrbinary()&lt;/EM&gt; function. &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.5;"&gt;This is because the previous code block has an error condition and you call the &lt;EM&gt;f.quit()&lt;/EM&gt; function. &lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.5;"&gt;The issue is that the FTP directory does not exist. &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.5;"&gt;The error is the &lt;EM&gt;f.quit()&lt;/EM&gt; function explicitly sets the sock object to None, then your code attempts to use it in the next block.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.5;"&gt;You should modify your code to exit if an error occurs or place conditional statements around code blocks to ensure that any pre-conditions are met.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.5;"&gt;Also, there are a few missing &lt;EM&gt;else&lt;/EM&gt; statements that will when messages are printed, for example:&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.cwd(DIRN) 
except ftplib.error_perm: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot CD to "%s"'&amp;nbsp; % DIRN 
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.quit() 
else: # &amp;lt;-- missing
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '*** Changed to "%s" folder' % DIRN&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:37:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110338#M8576</guid>
      <dc:creator>OwenEarley</dc:creator>
      <dc:date>2021-12-11T06:37:31Z</dc:date>
    </item>
    <item>
      <title>Re: Retrbindary code not working ?</title>
      <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110339#M8577</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 23:15:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110339#M8577</guid>
      <dc:creator>DEAF_PROBERT_68</dc:creator>
      <dc:date>2015-03-15T23:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Retrbindary code not working ?</title>
      <link>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110340#M8578</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you all.&lt;/P&gt;&lt;P&gt;First of all,&lt;/P&gt;&lt;P&gt;The file for the FTP server I have checked is no longer there or may have been moved; however, I tweaked and change to a different file to test it. The script is working now . If you compare the scripts I post earlier and the change I made....&lt;/P&gt;&lt;P&gt;I would like to Thank to Owen Earley for pointing me to the missing "else".&amp;nbsp; That worked !&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import modules
import ftplib
import os
import socket
from ftplib import FTP


HOST = "ftp.nifc.gov"
DIRN = "Incident_Specific_Data/ALASKA/DataRequests/"
FILE = "AK_UIA_polygon_data.gdb.zip"


# Create a block to connection or not


try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "ERROR: cannot reach" % HOST
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print '"***Connected to host "%s"' % HOST


# to log in anonymously
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.login()
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "ERROR: cannot login anonymously"
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.quit()
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '***Logged in as anonymous'


# To change to the directory in DIRN
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.cwd(DIRN)
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'ERROR: cannot CD to" "%s'&amp;nbsp; % DIRN
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.quit()
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '***Changed to "%s" folder' % DIRN


# Use the FTP Directory to retrieve KMZ
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)
except ftplib.error_perm:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "ERROR: cannot read file" % FILE
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink(FILE)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "***Downloaded %s to CWD" % FILE


# Disconnect from FTP
f.quit()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:37:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/retrbindary-code-not-working/m-p/110340#M8578</guid>
      <dc:creator>DEAF_PROBERT_68</dc:creator>
      <dc:date>2021-12-11T06:37:34Z</dc:date>
    </item>
  </channel>
</rss>

