Select to view content in your preferred language

ftp download script

2587
8
10-26-2020 10:23 AM
TonyAlmeida
Frequent Contributor

I upgraded to 3.6 python and now I can't run my script that runs fine in 2.7, I am not sure why, any ideas?

current code

import arcpy
import os
import os.path
from ftplib import FTP

directory = "\\maps" #location of folder on ftp
copy_files = "*.pdf" # This is the files i would like to copy from the ftp site

folder = "D:\\temp\\temp\\"

out_path = os.path.join(folder)

#FTP logon
ftp = FTP("ftp.proper.com")
ftp.login('blahuser', 'blahpass')

# navigate folder
ftp.cwd(os.path.join(directory))
print ("Changed to " + os.path.join(directory, copy_files))

#Now get a list of all files in the folder
filenames = ftp.nlst()
print (filenames)

#and loop through the filenames to download the files to your local 'folder'
for f in filenames:
    with open(os.path.join(out_path, f), 'wb') as local_file:
        ftp.retrbinary('RETR '+ f, local_file.write)

ftp.close()
print ("closed ftp connection")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Error

Traceback (most recent call last):
  File "C:/Python Scripts/FPT/TransferFromFtp.py", line 15, in <module>
    ftp.login('blahuser', 'blahpass')
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\ftplib.py", line 420, in login
    resp = self.sendcmd('PASS ' + passwd)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\ftplib.py", line 273, in sendcmd
    return self.getresp()
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\ftplib.py", line 246, in getresp
    raise error_perm(resp)
ftplib.error_perm: 530 Not logged in.‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
8 Replies
JoshuaBixby
MVP Esteemed Contributor

The error message is saying you aren't logged in.  Have the credentials possibly changed recently?

0 Kudos
TonyAlmeida
Frequent Contributor

Nope, I can ran that same script in 2.7 just fine. Then run it in 3.6 and gives me that error.

0 Kudos
GeoJosh
Esri Regular Contributor

Hi Tony,

I'm wondering if you need to keep the connection to the FTP open while you login, as demonstrated in the documentation for the ftplib module.

ftplib — FTP protocol client — Python 3.9.0 documentation 

Something like this:

with FTP("ftp.proper.com"as ftp:
    ftp.login('blahuser''blahpass')
    # ... do more stuff
0 Kudos
TonyAlmeida
Frequent Contributor

Made the change and still gives me the same error

import arcpy
import os
import os.path
from ftplib import FTP

directory = "\\maps" #location of folder on ftp
copy_files = "*.pdf" # This is the files i would like to copy from the ftp site

folder = "D:\\temp\\temp\\"

out_path = os.path.join(folder)

#FTP logon
with FTP("ftp.proper.com") as ftp:
    ftp.login("blahuser", "blahpass")


    # navigate folder
    ftp.cwd(os.path.join(directory))
    print ("Changed to " + os.path.join(directory, copy_files))

    #Now get a list of all files in the folder
    filenames = ftp.nlst()
    print (filenames)

    #and loop through the filenames to download the files to your local 'folder'
    for f in filenames:
        with open(os.path.join(out_path, f), 'wb') as local_file:
            ftp.retrbinary('RETR '+ f, local_file.write)

    ftp.close()
print ("closed ftp connection")
0 Kudos
DonShawEsri
Esri Contributor

Hi Tony,

I'd like to test the ftplib library in your Python 3 environment with an open FTP site to make sure that it can indeed connect to FTP sites. Does the login validate? Does the directory print?

from ftplib import FTP
ftp = FTP('ftp2.census.gov')

print(ftp.login())‍‍‍

print(ftp.dir())
0 Kudos
TonyAlmeida
Frequent Contributor

Yes it did.

230-Server: ftp2.census.gov
230-
230-Personal Identifiable Information (PII) shall not be placed on the FTP
230-server without prior special arrangement and in conjunction with ITSO.
230-
230-NOTE: The data available for anonymous FTP download on this FTP server are
230-also available over the Web:
230-http://www2.census.gov
230 Login successful.

TonyAlmeida
Frequent Contributor

Not sure what happened but I re-started my pc and code ran fine. Not sure what held it up.

DonShawEsri
Esri Contributor

Chalk it up to being Monday... glad it worked!

0 Kudos