Python ftplib - uploading multiple files?

Phil picture Phil · Jul 10, 2009 · Viewed 12.4k times · Source

I've googled but I could only find how to upload one file... and I'm trying to upload all files from local directory to remote ftp directory. Any ideas how to achieve this?

Answer

SilentGhost picture SilentGhost · Jul 10, 2009

with the loop?

edit: in universal case uploading only files would look like this:

import os
for root, dirs, files in os.walk('path/to/local/dir'):
    for fname in files:
        full_fname = os.path.join(root, fname)
        ftp.storbinary('STOR remote/dir' + fname, open(full_fname, 'rb'))

Obviously, you need to look out for name collisions if you're just preserving file names like this.