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?

6
  • 1
    first thing I notice is you're not putting any space so the command after the string concat would be scp\local\path\user@remoteip:\path\to\copy\onremote Commented Mar 11, 2018 at 16:57
  • 1
    sorry, that's there, let me correct it. Commented Mar 11, 2018 at 16:58
  • What is the value of 'send' debugger shows? It seems to me that there are no spaces before and after 'direct'. Commented Mar 11, 2018 at 16:59
  • also I would use subprocess.call or subprocess.check_call Commented Mar 11, 2018 at 17:02
  • 1
    the value is: file not found @AlexanderChingarev Commented Mar 11, 2018 at 17:07

2 Answers 2

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])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but it's not working still... let me add the error to the question
Oh! my bad. I use call() wrong. All characters separated by a space needs to be its own list entity, I'll update my answer
Thank you! It seems my problem, and formerly yours, was splitting the string! Good Day!
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).

1 Comment

Thanks, but that wasn't the problem for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.