1

Currently I am doing below task.

  1. Create a small file in Windows using Python
  2. Transfer this file to remote Unix environment using Paramiko Python library SFTP module

Problem faced:

Newline created on Windows is not properly converted to Unix new line character during the transfer. I see ^M characters inside the file on my remote host.

Any idea or suggestion how can I get rid of this behaviour.

When I do the transfer of the same file using WinSCP, I do not face this problem. I guess WinSCP has a built-in capability to handle this.

Python version used 3.5.

Step 1:

with open(myNewFile.txt,'w') as fileToUpload:
    fileToUpload.write('MyOwnTxt'+'\n')

Step 2:

COMP = remoteServerHost
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(COMP, username=user, password=password, allow_agent = False)
src = myNewFile.txt
dst = "/remotePath/myNewFile.txt"
ftp = ssh.open_sftp()
ftp.put(src , dst)
ftp.close()
ssh.close()

I am little surprised because no one has specified this problem in SO or any other forum. So it makes me feel is there something wrong on my Windows machine :(

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
just10minutes
  • 583
  • 10
  • 26

1 Answers1

2

Paramiko does not support converting EOL sequences of transferred files.

You have to convert the contents before uploading it.

See Define transfer mode when trying to SFTP files using Python.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    Thank you @Martin, I accept this answer for my question. I learnt that Winscp is able to do the conversion because it has the option of Text transfer where as paramiko just has binary transfer. – just10minutes Jul 08 '17 at 23:54