How to create a .csv or .xls file on ftp directly from Pandas dataframe as I do not want to create one on local machine and then push it on ftp. Many thanks in advance :)
As the docs say:
Store a file in binary transfer mode. cmd should be an appropriate
STOR
command:"STOR filename"
. file is a file object (opened in binary mode) which is read until EOF using itsread()
method in blocks of size blocksize to provide the data to be stored…
So, you need to give it a file-like object with an appropriate read
method.
A string is not a file-like object, but an io.BytesIO
is. So:
from ftplib import *
from StringIO import StringIO
import pandas
import io
ftp = FTP('ftp.mysite.com')
ftp.login('un', 'pw')
ftp.cwd('/')
buffer = StringIO.StringIO()
your_pandas_df.to_csv(buffer)
text = buffer.getvalue()
bio = io.BytesIO(str.encode(text))
ftp.storbinary('STOR filename.csv', bio)
for reference you can refer Python write create file directly in FTP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With