Home > Mobile >  How to put data into a tempfile and post as csv on sftp
How to put data into a tempfile and post as csv on sftp

Time:11-17

Goal is

Create a temporary csv file filled with data and upload it to an sftp. The data to fill is TheList and is from class 'list'.

What I am able to achieve

  • Create the connection to the sftp
  • Push a file to the sftp

What happens with the code below

There is a file created/put to the sftp, but the file is empty and has 0 byte.

Question

How can I achieve that I have a file with type csv on sftp with the content of TheList?

import paramiko
import tempfile
import csv

# code part to make and open sftp connection

TheList = [['name', 'address'], [ 'peter', 'london']]

csvfile = tempfile.NamedTemporaryFile(suffix='.csv', mode='w', delete=False)
filewriter = csv.writer(csvfile)
filewriter.writerows(TheList)
sftp.put(csvfile.name, SftpPath   "anewfile.csv")

# code part to close sftp connection

CodePudding user response:

You do not need to create a temporary file. You can use csv.writer to write the rows directly to the SFTP with use of file-like object opened using SFTPClient.open:

with sftp.open(SftpPath   "anewfile.csv", mode='w', bufsize=32768) as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    filewriter.writerows(TheList)

See also pysftp putfo creates an empty file on SFTP server but not streaming the content from StringIO

  • Related