<?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 Re: Fetch files from FTP with Python in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/fetch-files-from-ftp-with-python/m-p/223595#M7713</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Sonia,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would like to try and run your code through however am having problems with this snippet....Are you able to assist?&amp;nbsp; Should there be any more code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# This will handle the data being downloaded&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def handleDownload(block):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;file.write(block)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print ".",&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Nick&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 16 Sep 2011 13:45:58 GMT</pubDate>
    <dc:creator>NickDeMerchant</dc:creator>
    <dc:date>2011-09-16T13:45:58Z</dc:date>
    <item>
      <title>Fetch files from FTP with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/fetch-files-from-ftp-with-python/m-p/223594#M7712</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all you Pythoners,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am fairly new to this , but using online resources I have put together a script that fetches one file from an ftp connection (Python 2.5 on windows xp). I would like to download all the files within a specific directory but I am having trouble with the syntax of for/batch/iteration loop. This is what I have so far:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-----------------------------------------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Import the FTP object from ftplib&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from ftplib import FTP&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import os, sys&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# This will handle the data being downloaded&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def handleDownload(block):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; file.write(block)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print ".",&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Create an instance of the FTP object&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# FTP('hostname', 'username', 'password')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ddir='C:\\GISData\\test\\'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;os.chdir(ddir)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ftp = FTP('ftptest.int')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Log in to the server&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print 'Logging in.'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# You can specify username and password here :&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ftp.login('user1', 'testpassword') &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# This is the directory that we want to go to&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;directory = '\\GIS\\test\\datatest'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Change to that directory.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print 'Changing to ' + directory&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ftp.cwd(directory)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Print the contents of the directory&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ftp.retrlines('LIST')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# file name to acquire.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;filename = 'boundary.shp'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#for file in directory:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Open the file for writing in binary mode&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print 'Opening local file ' + filename&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;file = open(filename, 'wb')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Download the file a chunk at a time&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Each chunk is sent to handleDownload&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# We append the chunk to the file and then print a '.' for progress&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# RETR is an FTP command&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print 'Getting ' + filename&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ftp.retrbinary('RETR ' + filename, handleDownload)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Clean up &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print 'Closing file ' + filename&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;file.close()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print 'Closing FTP connection'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ftp.close()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;----------------------------------------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Mar 2011 12:44:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/fetch-files-from-ftp-with-python/m-p/223594#M7712</guid>
      <dc:creator>SoniaSandiago</dc:creator>
      <dc:date>2011-03-01T12:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Fetch files from FTP with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/fetch-files-from-ftp-with-python/m-p/223595#M7713</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Sonia,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would like to try and run your code through however am having problems with this snippet....Are you able to assist?&amp;nbsp; Should there be any more code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# This will handle the data being downloaded&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def handleDownload(block):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;file.write(block)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print ".",&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Nick&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Sep 2011 13:45:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/fetch-files-from-ftp-with-python/m-p/223595#M7713</guid>
      <dc:creator>NickDeMerchant</dc:creator>
      <dc:date>2011-09-16T13:45:58Z</dc:date>
    </item>
  </channel>
</rss>

