Reading .csv file to memory from SFTP server using Python Paramiko

Viewed 6983

I am trying to read a CSV file on the SFTP in my Python memory. I tried the following, which works fine for a FTP connection, but not for a SFTP.

E.g., I want to replicate:

df = pd.read_csv(...)

But without storing it first locally (reason being is because I want to run it as a Cloud Function and then I don't want local files in my cache).

How can I do it differently?

def read_file_sftp_local_memory(sftp, path, filename):

    flo = BytesIO()
    path_query = "".join(['RETR ', path, '/', filename])
    sftp.retrbinary(path_query, flo.write)
    flo.seek(0)
    return flo

I tried also the following:

def read_file_csv(sftp, path, filename):

    # Download
    sftp.get("/".join( os.path.join(path, filename) ), filename)

    # Read
    df = pd.read_csv(filename)

    # Delete
    os.remove(filename)

    # Return
    return df

But this error is returned:

raise IOError(text)
OSError: Failure
1 Answers
Related