shriyaj
shriyaj

Reputation: 33

Delete files from ftp after download

I am newbie to python and learning to delete the file which are downloaded from ftp site.

this is my error :OSError: [Errno 2] No such file or directory: 'RingGoData-2014-07-02.csv'

this is my code:

ftp = ftplib.FTP('192.198.0.20', 'bingo', 'Password')
files = ftp.dir('/')
ftp.cwd("/")
#ftp.retrlines('LIST')
filematch = '*.csv'
target_dir = '/home/toor/ringolist'
import os

for filename in ftp.nlst(filematch):
    target_file_name = os.path.join(target_dir,os.path.basename(filename))
    with open(target_file_name,'wb') as fhandle:
         ftp.retrbinary('RETR %s' % filename, fhandle.write)
        if os.path.isdir(filename)== True:
             shutil.rmtree(filename)
         else:
             os.remove(filename)

Upvotes: 0

Views: 3818

Answers (1)

RvdK
RvdK

Reputation: 19790

Shutil and os.remove work on your current filesystem, NOT on the FTP server. Your trying to delete a local file which isn't there. You should use FTP.delete(filename)

Upvotes: 4

Related Questions