Phil
Phil

Reputation: 3648

Python ftplib - uploading multiple files?

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?

Upvotes: 5

Views: 15676

Answers (3)

Toulon7559
Toulon7559

Reputation: 31

Look at Python-scriptlines required to make upload-files from JSON-Call and next FTPlib-operation: why some uploads, but others not?

Although a different starting position than your question, in the Answer of that first url you see an example construction to upload by ftplib a json-file plus an xml-file: look at scriptline 024 and further.

In the second url you see some other aspects related to upload of more files.

Also applicable for other file-types than json and xml, obviously with a different 'entry' before the 2 final sections which define and realize the FTP_Upload-function.

Upvotes: 0

SilentGhost
SilentGhost

Reputation: 319551

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.

Upvotes: 16

blispr
blispr

Reputation: 912

Create a FTP batch file (with a list of files that you need to transfer). Use python to execute ftp.exe with the "-s" option and pass in the list of files.

This is kludgy but apparently the FTPlib does not have accept multiple files in its STOR command.

Here is a sample ftp batch file.

*

OPEN inetxxx 
myuser mypasswd 
binary 
prompt off 
cd ~/my_reg/cronjobs/k_load/incoming 
mput *.csv 
bye
  • If the above contents were in a file called "abc.ftp" - then my ftp command would be

    ftp -s abc.ftp

Hope that helps.

Upvotes: -3

Related Questions