Python sending files over scp using pexpect
<p>So I'm trying to send all my torrent files to a server with scp using pexpect.  This works for the most part but not all my torrent files get sent.  Even though the print function in 'if i==0' prints out all the torrents.  And if I go to the server and delete everything that got sent and run it again, the same torrent files get sent.  Does anyone know what the problem might be?</p>

<pre><code>import os
import getpass
import subprocess
import pexpect

filepath = 'my downloads dir'

os.chdir(filepath)

var_password = str(getpass.getpass())
destination = "user@server:dir"

for filename in os.listdir():
    if filename.endswith('.torrent'):
        try:
            var_command = "scp " + filepath + filename + " " + destination

            var_child = pexpect.spawn(var_command)
            i = var_child.expect(["password:", pexpect.EOF])

            if i==0: # send password            
                print('Sending: ' + filename)    
                var_child.sendline(var_password)
                var_child.expect(pexpect.EOF)
            elif i==1: 
                print("Got the key or connection timeout")
                pass
        except Exception as e:
            print("Oops Something went wrong buddy")
            print(e)
</code></pre>