Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to specify local destination folder when retrieving files from FTP

Current code will download remote XML files to the directory where this program located. How can I specify another local directory as a destination?

Please also point me if there any strange code in here. :)

import ftplib
import os
import os
import socket

HOST = 'ftp.server.com'
DIRN = 'DirectoryInFTPServer'
filematch = '*.xml'
username = 'username'
password = 'password'

def main():
    try:
        f = ftplib.FTP(HOST)
    except (socket.error, socket.gaierror), e:
        print 'ERROR: cannot reach "%s"' % HOST
        return
    print '*** Connected to host "%s"' % HOST

    try:
        f.login(username, password)
    except ftplib.error_perm, e:
        print 'ERROR: cannot login'
        f.quit
        return
    print '*** Logged in successfully'

    try:
        f.cwd(DIRN)
    except ftplib.error_perm, e:
        print 'ERROR: cannot CD to "%s"' % DIRN
        f.quit()
    print '*** Changed to folder: "%s"' % DIRN

    try:
        s = 0;
        for filename in f.nlst(filematch):
            fhandle = open(filename, 'wb')
            print 'Getting ' + filename
            f.retrbinary('RETR ' + filename, fhandle.write)
            s = s + 1
    except ftplib.error_perm, e:
        print 'ERROR: cannot read file "%s"' % filename
        os.unlink(filename)

    f.quit()
    print 'Files downloaded: ' + str(s)
    return

if __name__ == '__main__':
    main()
like image 364
Askar Avatar asked Apr 08 '13 07:04

Askar


2 Answers

Use os.chdir() to change the local working directory, and then change it back after fetching the files.

I have marked the added lines with a ####

import ftplib
import os
import os
import socket

HOST = 'ftp.server.com'
DIRN = 'DirectoryInFTPServer'
filematch = '*.xml'
username = 'username'
password = 'password'
storetodir='DirectoryToStoreFilesIn' ####
def main():
    try:
        f = ftplib.FTP(HOST)
    except (socket.error, socket.gaierror), e:
        print 'ERROR: cannot reach "%s"' % HOST
        return
    print '*** Connected to host "%s"' % HOST

    try:
        f.login(username, password)
    except ftplib.error_perm, e:
        print 'ERROR: cannot login'
        f.quit
        return
    print '*** Logged in successfully'

    try:
        f.cwd(DIRN)
    except ftplib.error_perm, e:
        print 'ERROR: cannot CD to "%s"' % DIRN
        f.quit()
    print '*** Changed to folder: "%s"' % DIRN

    currdir=os.getcwd() ####

    try:
        os.chdir(storetodir)####
        s = 0;

        for filename in f.nlst(filematch):
            fhandle = open(filename, 'wb')
            print 'Getting ' + filename
            f.retrbinary('RETR ' + filename, fhandle.write)
            s = s + 1
    except ftplib.error_perm, e:
        print 'ERROR: cannot read file "%s"' % filename
        os.unlink(filename)
    os.chdir(currdir) ####
    f.quit()
    print 'Files downloaded: ' + str(s)
    return

if __name__ == '__main__':
    main()
like image 124
Manishearth Avatar answered Oct 26 '22 23:10

Manishearth


The default behavior of ftplib is to copy all files from the server into the current working directory (CWD).

To specify where the files go, you can temporarily modify the CWD using os.chdir() and you can find the current CWD using os.getcwd()


Example of use:

>>> import os
>>> os.getcwd()
'C:\\python'
>>> tmp_a = os.getcwd()
>>> os.chdir('C:/temp')
>>> os.getcwd()
'C:\\temp'
>>> os.chdir(tmp_a)
>>> os.getcwd()
'C:\\python'
like image 34
Inbar Rose Avatar answered Oct 27 '22 01:10

Inbar Rose