Download files from ftp

2142
2
Jump to solution
12-12-2018 11:15 AM
TonyAlmeida
Occasional Contributor II

I am trying to download all the pdf's from an ftp.

I get the following error and from what i found the error means.

"The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource."

How else can one download from a ftp server?

I appreciate the help!

import os
import os.path
from ftplib import FTP

directory = "\\blah" #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.blah.blah")
ftp.login("blah1", "blah2")

# navigate folder
ftp.cwd(os.path.join(directory, copy_files))
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 "D:\GIS Folder\Python Scripts\downloadftp\downloadftp.py", line 17, in <module>
    ftp.cwd(os.path.join(directory, copy_files))
  File "C:\Python27\ArcGIS10.6\lib\ftplib.py", line 574, in cwd
    return self.voidcmd(cmd)
  File "C:\Python27\ArcGIS10.6\lib\ftplib.py", line 256, in voidcmd
    return self.voidresp()
  File "C:\Python27\ArcGIS10.6\lib\ftplib.py", line 231, in voidresp
    resp = self.getresp()
  File "C:\Python27\ArcGIS10.6\lib\ftplib.py", line 226, in getresp
    raise error_perm, resp
error_perm: 501 Syntax error in parameters or arguments.
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

It seems ftp.cwd() takes a directory as the parameter, not a file path.

View solution in original post

2 Replies
DarrenWiens2
MVP Honored Contributor

It seems ftp.cwd() takes a directory as the parameter, not a file path.

TonyAlmeida
Occasional Contributor II

You are correct. i changed line 17 to ftp.cwd(os.path.join(directory)) and works, thanks!

0 Kudos