简体   繁体   中英

"Permission denied" error from downloading all files from FTP folder

So far I have the gotten the names of the files I need from the FTP site. See code below.

from ftplib import FTP
import os, sys, os.path

def handleDownload(block):
     file.write(block)

ddir='U:/Test Folder'
os.chdir(ddir)
ftp = FTP('sidads.colorado.edu')
ftp.login()

print ('Logging in.')
directory = '/pub/DATASETS/NOAA/G02158/unmasked/2012/04_Apr/'

print ('Changing to ' + directory)
ftp.cwd(directory)
ftp.retrlines('LIST')

print ('Accessing files')

filenames = ftp.nlst() # get filenames within the directory
print (filenames)

Where I am running into trouble is the download of the files into a folder. The code below is something I have tried however I receive the permission error due to the file not being created before I write to it.

for filename in filenames:
     local_filename = os.path.join('C:/ArcGis/New folder', filename)
     file = open(local_filename, 'wb')
     ftp.retrbinary('RETR '+ filename, file.write)
     file.close()

ftp.quit()

Here is the error and callback.

在此处输入图像描述

The directory listing includes the . reference to the folder (and probably also .. reference to the parent folder).

You have to skip it, you cannot download it (them).

for filename in filenames:
    if (filename != '.') and (filename != '..'):
        local_filename = os.path.join('C:/ArcGis/New folder', filename)
        file = open(local_filename, 'wb')
        ftp.retrbinary('RETR '+ filename, file.write)
        file.close()

Actually you have to skip all folders in the listing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM