-1

Firstly, please excuse my lack of knowledge; I am a complete beginner at python.

I would like to run several command line commands from within a python script, using the subprocess module. My python script is supposed to be an application that downloads files from a url. These commands are shown below:

set /P _inputname= my-url-here
wget %_inputname%

(wget is in the same directory as my python script)

I have tried using this method:

cmds = ['set /P _inputname=', 'my-url-here', 'wget %_inputname%']
subprocess.run('cmds', shell=True)

However, it fails to run wget.

I understand that my question may seem very similar to this question and this question. However, the solutions in the aforementioned posts have not worked for me. Is there an alternative method?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
JS4137
  • 314
  • 2
  • 11

1 Answers1

1

The posts you mentioned were meant for systems with bash installed (such as most linux distros and other unix like oses)

Though I do not own a windows machine, I presume this will work for you:

(use the & sign to seperate commands)

cmds = ['set', "/P", '_inputname={}'.format('my-url-here'), '&','wget', '%_inputname%']
subprocess.run(cmds, shell=True)

note also that cmds is the variable cmds, not the string

source: How do I run two commands in one line in Windows CMD?

jonathan
  • 590
  • 3
  • 14
  • Given that source, why do you believe this should be a list rather than a single string? – Charles Duffy Apr 13 '20 at 19:13
  • because "cmds" is not a command, and he specifies the list of commands in his example. I assume he wanted to execute those commands and not the command "cmds" which isn't one. Especially since the name identical to the name he gave his list variable. – jonathan Apr 13 '20 at 19:17
  • @jonathan This solution does partially work, however it causes the error `The system cannot find the path specified`, even when I specify a path – JS4137 Apr 13 '20 at 19:45
  • I notice now I made a mistake. The & should be before wget of course. That could be the issue. Apart from that, some path might not be found. Like wget? is it installed (or is it default by now on windows) – jonathan Apr 13 '20 at 19:48
  • @jonathan Thank you for your response. `wget` is in the same directory as my python script, so I am not sure which path causes the issue. – JS4137 Apr 13 '20 at 20:01
  • Interesting. You might then need something like this: (edited post) Notice that the _intputname=xxx is now one part. – jonathan Apr 13 '20 at 20:03
  • Booted up a windows vm, think I found your issue. Edited the command again. Basically the thing thought "wget %inputname%" was one command, while it's two parts. Should be seperated by a comma in python. That's why you use a list in the first place. – jonathan Apr 13 '20 at 20:21
  • Thanks a lot, it solved the path issue. However, now it has this error: `URL "%_inputname%" lacks a protocol`. – JS4137 Apr 13 '20 at 20:56
  • you need http:// in front of it (or another protocol. https, ftp, ssh, etc) – jonathan Apr 13 '20 at 21:00
  • @jonathan, ...caveat here is that Windows isn't my platform, but in the UNIX world, when you provide a list to `subprocess` with `shell=True`, only the first element in that list is treated as code for the shell to execute, and everything else is treated as an argument to that code. Hence why we really need to be referring to Windows-aware documentation, *or* testing on a Windows system. – Charles Duffy Apr 13 '20 at 21:20
  • I did test on a windows system, booted a vm (as mentioned) :) – jonathan Apr 13 '20 at 21:59