1

I'm trying to get a script to run on Bash to transfer files from my local machine to a remote computer on my network. The bash command is :

scp \local\path\ user@remoteip:\path\to\copy\onremote

This has worked alright so far, so I thought I'd automate it using a python script. This is what I came up with:

import subprocess
direct = input("Enter path to directory: ")
send = "scp " + direct + " user@remoteip:\path\to\copy\onremote"
subprocess.Popen(send)

For some reason, It's not working, any ideas?

rawplutonium
  • 386
  • 5
  • 15

2 Answers2

2

The problem here is that Popen() expects a list. So the solution is to split the string on its spaces, formatted = send.split(" "). As per the questions around using Popen() or call(), here is an excellent answer detailing the differences.What's the difference between subprocess Popen and call (how can I use them)?

Here is something that works for me, using subprocess call().

from subprocess import call
direct = input("Enter path to directory: ")
send = "scp" + direct + " user@remoteip:\path\to\copy\onremote"
formatted = send.split(" ")
call([formatted])
pastaleg
  • 1,782
  • 2
  • 17
  • 23
1

It is recommended that you pass in a list of arguments to the
subprocess.Popen(["scp", "-r", "path", ...]).

However, if you still want to pass in a whole string, pass an optional shell parameter like
subprocess.Popen(send, shell=True).

postmalloc
  • 90
  • 1
  • 8