2

I am trying to download files from a ftp server but I am running into an error permission denied error.

Traceback (most recent call last):
   File "/Users/x34/Documents/Python/ftp_download.py", line 27, in <module>
     download()
   File "/Users/x34/Documents/Python/ftp_download.py", line 21, in download
     with open(filename,'wb') as f:
IOError: [Errno 13] Permission denied: '/p012r018_5dt19900722_z20_30.tif.gz'

Downloading manually or with filezilla works fine but my script below does not

from ftplib import ftp    
ftp = FTP(r'ftp.glcf.umd.edu')
ftp.login()

directory = 'glcf/Landsat/WRS2/p012/r018/p012r018_5dx19900722.TM-GLS1990'
filename = '/p012r018_5dt19900722_z20_30.tif.gz'

ftp.cwd(directory)

with open(filename,'wb') as f:
    ftp.retrbinary('RETR' + filename,f.write)

ftp.close()

One another note... and perhaps I misread the docs - http://docs.python.org/library/ftplib.html but I do not fully understand where ftplib decides to download the files (default download directory?). Is there another module better suited for this application?

UPDATE

I should clarify that the filename on the server does not contain the '/' in front. I simply added that as it appeared to help locate the correct file location and name as prior attempts ended in the following error ftplib.error_perm: 500 Unknown command.

the full path to the file is

 ftp.glcf.umd.edu/glcf/Landsat/WRS2/p012/r018/p012r018_5dx19900722.TM-GLS1990/p012r018_5dt19900722_z20_30.tif.gz'
GeoPy
  • 1,556
  • 3
  • 17
  • 21
  • 1
    It looks like your script is trying to save the downloaded content to a file at the root (`/`) of your filesystem. Do you have enough permissions there? It is trying to save the file with the original filename, and it is preceded by a `/`, thus creating the file at `/`. – Valdir Stumm Junior Aug 10 '12 at 23:43
  • the main reason to add the / in front was that it seemed like a step forward because before it was giving me another error this being the last part of that traceback --> "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 219, in getresp raise error_perm, resp ftplib.error_perm: 500 Unknown command. – GeoPy Aug 10 '12 at 23:58
  • 1
    Try adding a space character after "REPR" in your code. I've updated the code in the answer. You are concatenating "REPR" with the filename, but the correct would be: "REPR file.txt" – Valdir Stumm Junior Aug 11 '12 at 01:13

2 Answers2

7

Its saves your file where you ask it to save, in the line with open(filename,'wb') as f:, you are opening the file to save the received content.

And, as your filename starts with a /, it tries to save to the root (/) of your filesystem, where it looks like you don't have enough permissions.

Try this:

from ftplib import ftp    
ftp = FTP(r'ftp.glcf.umd.edu')
ftp.login()

directory = 'glcf/Landsat/WRS2/p012/r018/p012r018_5dx19900722.TM-GLS1990'
filename = '/p012r018_5dt19900722_z20_30.tif.gz'

ftp.cwd(directory)

with open(filename[1:],'wb') as f: # slices the string, "cutting" out the "/"
    ftp.retrbinary('RETR ' + filename,f.write)

ftp.close()

Notice that we changed the filename to be written in your filesystem (line with open(filename[1:],'wb') as f:). Take a look at this question, if you don't know the slice operator.

Also, you should put a space character in the end of the 'RETR' string in your code. It should be 'RETR ' + filename instead of 'RETR' + filename. 'RETR somefile.txt' is a command to the FTP server, and you were doing 'RETRsomefile.txt', corrupting the command with the filename.

Community
  • 1
  • 1
Valdir Stumm Junior
  • 4,568
  • 1
  • 23
  • 31
  • I tried your suggestions on both my pc and mac but to no avail as it gives me the error of "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 219, in getresp raise error_perm, resp ftplib.error_perm: 500 Unknown command ------ the ftp site is free to the public if anyone wants to try if its only my computers? – GeoPy Aug 11 '12 at 00:24
  • After correcting the code with the space at the end of 'RETR ', I am getting a permission error at the server. – Valdir Stumm Junior Aug 11 '12 at 01:19
0

You are using the same filename variable in two places where I assume the root "/" is valid for your ftp but obviously a permission issue locally.

Try using "/" in the ftp command, but without "/" on the local file you are opening

jdi
  • 90,542
  • 19
  • 167
  • 203