Jack Lim
Jack Lim

Reputation: 315

Python Paramiko SFTP Open File start from N-line

Because my file in the server is huge and I and to save the time to downloading the file from the server so I using paramiko sftp.open(file) to read the file in the server.

Is there any ways that we can start reading the file from any line of the file using sftp.open(file) or others?


This how I used to read the file from local directory

# open the file and start reading from the nth line
for line in itertools.islice(file, no_line, None):
    print(line)

This how I read the file from the server

ssh = paramiko.SSHClient()
ssh.connect(host, port, username, password)
readfile = ssh.open_sftp()
file = readfile.open(os.path.join(path, file))
for line in file:
    print(line)

Upvotes: 1

Views: 2100

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202168

There's no API in SFTP that would allow you to skip to N-th line.

You can start reading from N-th byte though, using SFTPFile.seek method:

file = readfile.open(os.path.join(path, file))
file.seek(offset)
for line in file:
    print(line)

Upvotes: 1

Related Questions