2017-06-01 87 views
0

我需要從服務器獲取文件,然後將它們寫入數據庫。 我使用:嘗試使用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。 如何從文件中獲取內容並將其寫入數據框?

+0

什麼返回'None'? – martineau

+0

@martineau文件的內容是空的,它返回'None',但它不會清空。我需要讀取並添加到數據框 –

+0

'all_files'的值是什麼? – errata

回答

0

如果我正確理解你的問題,你想從SFTP服務器讀取文件到一個變量/內存。

對於這一點,你需要使用.getfo,如:

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

或者,直接使用的paramiko庫(不pysftp包裝)。
參見Read a file from server with ssh using python

相關問題