0

First of all, I'm a noob with Python so this is gonna seem like a really easy question probably.

I'm trying to figure out how to use Python Paramiko, and have succeeded in connecting to my remote server, so I've succeeded in the first respect. What I would like to do now is to read a particular line from a file.

I've looked through Paramiko and it looks like the BufferedFile class might be able to read lines from the file. However I can't figure out how I need to load the file within the class. There is no 'open file' method, with the other methods just stating that they do things to 'the file'. The constructer doesn't take a file, so how do I load a file which I use the methods on? Probably a big thing I've missed, but I'm learning, so please excuse me.

4

1 回答 1

2

您可能想要使用 SFTP 子系统。

拥有Transport实例并成功通过身份验证后,获取SFTP客户端:

sftp_client = my_transport.open_sftp_client()

一旦你有了它,你可以使用以下open()方法打开一个远程文件:

my_file = sftp_client.open('remote_file.txt', 'r')

# use my_file, it is a file-like object, for example:
print my_file.read(100)

my_file.close()
于 2012-08-27T10:50:32.193 回答