I have to automatically upload folders to an FTP using a Python script. I am able to upload a single file, but not folders with subfolders and files in them. I did a lot of search, but failed. Could some one help me out here? Thanks in advance.
#! /usr/bin/python
import ftplib
s = ftplib.FTP('serverip','usrname','password')
file = '/home/rock/test.txt'
ftppath = '/IT'
filename = "rak"
s.cwd(ftppath)
f = open(file,'rb')
s.storbinary('STOR ' + filename, f)
f.close()
s.quit()
using ftputil:
It is easy to use lftp to upload folders to an FTP. I use this in my Python script to move folders to FTP
Python script: #! /usr/bin/python
ftp_script:
Maybe you try ftpsync.py. If this one doesn't helps, try google search on python ftpsync and you get a lot of answers.
I recently came into this problem and figured out a recursive function to solve it.
You basically need to use os.walk() to grab those files and transfer them.
Here's a script I wrote for myself to do much of what your asking. I wrote it a long time ago, so I'd probably do it differently if I wrote it again, but I get a lot of use out of it.
It imports psftplib, which is a wrapper I wrote for the putty sftp. Feel free to remove these references, or grab the lib at: http://code.google.com/p/psftplib/source/browse/trunk/psftplib.py
EDIT 20/12/2017:
I have written a project in GitHub for this purpose. Click for details!
There are good answers above but i also want to add a good one using ftputil package. If you need to upload files from local directory to ftp directory, you can use this recursive function:
If you decide to use this function, you have to connect ftp using ftputil package. For this, you can use this snippet:
So, we're almost done. The last thing is usage of the function for beginners like me:
The most important thing is "/" character at the end of paths. You need to put it at the end. Finally, i want to share entire code: