Friday 5 September 2014

Recursively Fetch File Paths from FTP

Get all file paths from a given root path, output in a text file.



from __future__ import print_function
import os
import time
import pysftp

ftp_username='xxx'
ftp_password='xxx'
ftp_host='xxx'

year = time.strftime("%Y")
month = time.strftime("%m")
day = time.strftime("%d")

ftp_dir = 'data/'+year+'/'+month

filename = time.strftime('ftp_file_lists.txt')
fout = open(filename, 'w')

wtcb = pysftp.WTCallbacks()

with pysftp.Connection(ftp_host, username=ftp_username, password=ftp_password) as sftp:
    sftp.walktree(ftp_dir, fcallback=wtcb.file_cb, dcallback=wtcb.dir_cb, ucallback=wtcb.unk_cb)
    print(len(wtcb.flist))
    for fpath in wtcb.flist:
        print(fpath, file=fout)
sftp.close()

Reference: https://bitbucket.org/dundeemt/pysftp/src/436d3395c177bcbfd289004b2066fc8471c4d3d7/tests/test_walktree.py?at=default

2 comments:

  1. Thanks, great example code.
    The call to sftp.close() is not necessary as the connection is closed automatically at the end of the with-block.

    ReplyDelete
  2. Thanks bro. Really great example code. It gave me a lot of insight

    ReplyDelete