Python ftplib - uploading multiple files?
Asked Answered
R

3

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?

Reprise answered 10/7, 2009 at 15:47 Comment(1)
gist.github.com/dnozay/37e8148ee748068afb91 - recursive copy.Memento
G
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.

Gneiss answered 10/7, 2009 at 15:48 Comment(2)
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/somedirectoryReprise
do you need to close the file?Yehudi
T
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.

Togs answered 11/2, 2017 at 13:4 Comment(0)
D
-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.

Dosimeter answered 10/7, 2009 at 16:49 Comment(2)
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 byeDosimeter
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.Whity

© 2022 - 2024 — McMap. All rights reserved.