尝试使用pysftp获取文件时获取无

时间:2017-06-01 14:12:58

标签: python sftp pysftp

我需要从服务器获取文件,然后将它们写入数据库。 我用:

with pysftp.Connection(host, username=username, password=password, cnopts=cnopts) as sftp:
    sftp.chdir('path')
    all_files = [file_name for file_name in sftp.listdir() if 'access' in file_name]
    today_log = all_files[0]
    all_files = all_files[1:]
    df = df.append(sftp.get(str(today_log)))

但它会返回None。 如何从文件中获取内容并将其写入数据框?

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您希望将SFTP服务器中的文件读入变量/内存。

为此,您需要使用.getfo,例如:

flo = BytesIO()
sftp.getfo(remotepath, flo)

或者,直接使用Paramiko库(不使用pysftp包装器) 请参阅Read a file from server with ssh using python