Reputation: 45
I have created a CSV file using the Python csv
library.
When all the CSV files are created, my second task is to put them on an FTP server.
To achieve this, I generate all my files in the /tmp
folder on my Linux computer, then I open an FTP connection and I upload them all to the remote server.
My question is: It is possible to create the CSV files in the memory buffer and put them directly on my FTP server?
Upvotes: 0
Views: 3101
Reputation: 641
Yes, you can generate files in memory using StringIO (or better cStringIO). You can try something like this:
import csv
from ftplib import FTP
from cStringIO import StringIO
...
ftp = FTP(host, user, passwd)
ftp.login()
output = StringIO()
data = csv.writer(output)
data.writerow(["data"] * 5)
output.seek(0)
ftp.storbinary("STOR data.txt", output)
...
Please note, this isn't fully workable code and you might need some changes depending on your FTP configuration. For example, I had to add ftp.set_pasv(False) to make it work with my test FTP.
Upvotes: 1