5

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?

Phil
  • 3,628
  • 8
  • 34
  • 36

3 Answers3

16

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.

dnozay
  • 23,846
  • 6
  • 82
  • 104
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • It's script that would upload website to host... so lets say i have website in some directory on my local hard drive and i want to upload its contents but NOT with directory, only files so my website after being uploaded will be accessible from myaddress.com instead of myaddress.com/somedirectory – Phil Jul 10 '09 at 15:54
  • do you need to close the file? – liang Sep 09 '18 at 11:38
0

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.

Community
  • 1
  • 1
Toulon7559
  • 31
  • 10
-3

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.

blispr
  • 883
  • 5
  • 10
  • I implied an myftp.ftp file as a 'batch' file and not an MSDOS specific ".bat" file. This file will contain a list of ftp commands (and not OS-specific commands). For example , here's one of mine - OPEN inetxxx myuser mypasswd binary prompt off cd ~/my_reg/cronjobs/k_load/incoming mput *.csv bye – blispr Jul 10 '09 at 19:14
  • this way is not portable, and why use another ftp client when Python has its own. For multiple, do it the way like SilentGhost did. – ghostdog74 Jul 11 '09 at 11:23