Shoes
Shoes

Reputation: 21

Paramiko Append/Edit specific line in a file on remote SFTP server

I need to append a line to a file on remote server using Paramiko.
I'using the following code.
The problem is that it appends the line at the bottom of the file, and the requirement is to append it in middle at a particular line.

Any clue/help will be hugely appreciated

ftp = ssh_client.open_sftp()
file=ftp.file('file_name', "a", -1)
file.write('appending_line')
file.flush()
ftp.close()

Upvotes: 2

Views: 1571

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202168

You cannot append to a line in a middle of a file even with a local file, let alone with a remote file.

You have to read/download the whole file (or at least the part starting with the line to be modified), modify the contents as you need, and write/upload whole file back again.

For that see:
Editing specific line in text file in Python

You just need to replace the plain open with Paramiko SFTPClient.open (or its .file alias).

Upvotes: 1

Related Questions