1

I've spent hours messing with this, scouring the web, and generally finding no luck. Here's the setup:

  • Windows 10
  • Python 3 (playing in REPL w/ VS Code)
  • SFTP server authenticating via password only
  • Large 1+ gigabyte file to PUT

Initially I wrote a little Pysftp (Paramiko) code:

import pysftp
with pysftp.Connection(host=host, username=username,
                       password=password, cnopts=sftp_options) as sftp:
    sftp.put(localpath, remotepath, confirm=True)

That worked for smaller files when testing. It also succeeded running with a large file but then no file could be found when looking back at the folder. My groundless hunch is that it somehow took too long and Paramiko unwittingly shot the bits into a void.

I turned to the sftp command-line utility which conveniently works on Windows 10. I was able to manually test that uploading works like so:

sftp username@host
Welcome to this funky Secure FTP Server. Please enter your password:
sftp> PUT localpath remotepath
sftp> QUIT

The key problem was the password input, obviously, and I bent over backwards trying to automate this with Popen(). I read and ruminated over these on StackOverflow: Use subprocess to send a password and Python: How to read stdout of subprocess in a nonblocking way. None of this playing resulted in anything.

I was able to read output but input always seemed to come from my keyboard only. Then I tried Pexpect which also did not work because on Windows it operates differently and won't work on some programs:

PopenSpawn is not a direct replacement for spawn. Many programs only offer interactive behaviour if they detect that they are running in a terminal. When run by PopenSpawn, they may behave differently.


  • UPDATE 11/19: I was able to execute PuTTY's command-line psftp.exe which accepts password as a parameter. I also confirmed that Pysftp/Paramiko with that large file worked fine against an SFTP server I setup myself so it's something to do with the vendor's server (nothing I can do anything about). I'll continue plugging at this and hopefully have a comprehensive answer to post here.

Aside from resorting to Ubuntu via WSL, does anyone have any ideas for how to upload a file to an SFTP server using Python 3.x on Windows?

Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112

0 Answers0