0

This is a follow up on a previous question as I have made progress(which is irrelevant at this point). It is worth noting that I am learning python and barely know what I am doing, however, I am familiar with programming. I am trying to call an SCP command in the windows terminal through python. However, it is not doing the desired effect. The script runs smoothly with no errors and it prints the debug commands as I have written them. However, the SCP call does not actually go through on the other end. To make sure I have the right command, I have it set to print the same command that it called afterwards. When I copy this printed command and paste it into the windows command terminal, it gives the desired effect. Why is the same command not working correctly in python? Thanks. This is my script:

import subprocess

subprocess.run(['scp', 'c:/users/<name>/desktop/OOGA.txt', 'pi@<IP>:here/'], shell=True)
print ("done")
print ('scp', 'c:/users/<name>/desktop/OOGA.txt', 'pi@<IP>:here/')
  • `shell=True` is meaningless when the argument is a list. – Barmar Jan 09 '20 at 00:49
  • When I don't use `shell=True` I get a long error which was the problem of my previous question. I did not get a clear answer and the only way I found that works was to add this argument. – Rymazon Jan 09 '20 at 01:00
  • The first argument should either be a list of the form `[command, arg1, arg2, ...]` or a single string with full command line. When it's a single string, `shell=True` says whether it should use a shell to parse it. Your previous question had a single string in a list, which is wrong. – Barmar Jan 09 '20 at 01:02
  • I've added an answer to the previous question to that effect. – chepner Jan 09 '20 at 01:05
  • Thanks for the help. Unfortunately, using a single string with `shell=true` results in the same result as this question and splitting it into a string without `shell=true` gives the same error as the other one. – Rymazon Jan 09 '20 at 01:45
  • don't you have to login to send it ? – furas Jan 09 '20 at 02:25
  • I don't know what shell it uses on Windows but on Linux it uses `/bin/sh` instead of /bin/bash` and it sometimes makes problem. It can be changed using executabe='/bin/bash'. You can also put command in file and run it as script - something like `run("cmd script.bat")` – furas Jan 09 '20 at 02:32

1 Answers1

0

Try using raw string if shell is set to True:

from subprocess import run as subrun

status = subrun(r'scp c:/users/<name>/desktop/OOGA.txt pi@<IP>:here/',shell=True)
print("Done")
print(status)         
code pinguin
  • 94
  • 1
  • 9