-1

Is there a way you can use a full wget command into python?

I know that we can do this: os.system('wget %s' %%url)

But I want a full command with all of the data saved into a directory: wget -r --accept "*.exe,*.dll,*.zip,*.msi,*.rar,*.iso" ftp://ftp.apple.asimov.com/ -P e:\e

Kara
  • 6,115
  • 16
  • 50
  • 57
user1067332
  • 15
  • 2
  • 6

1 Answers1

0

There is the subprocess module for that (this is what os.system calls but with a bit more flexibility). Specifically, you can use the call function in the following way to execute any command

import subprocess
subprocess.call(r'wget -r --accept "*.exe,*.dll,*.zip,*.msi,*.rar,*.iso" ftp://ftp.apple.asimov.com/ -P e:\e', shell=True)

Alternatively, you can pass individual arguments as a list omitting the shell flag:

 subprocess.call(['wget', '-r', ...])

Also check the return value for errors. For details, see the standard library documentation on subprocess.

Emilia Bopp
  • 866
  • 10
  • 20
  • Is there a way to impalement a progress bar? That shows the downloading process? – user1067332 Apr 03 '14 at 15:05
  • Sure, since wget can do this you just need to pipe its output to the console. Look at the stdout parameter of the call function. – Emilia Bopp Apr 03 '14 at 15:12
  • Just piping the stderr/stdout of a command like wget probably isn't going to work well. You'll get better results by using the pexpect/wexpect module. See this answer: http://stackoverflow.com/a/1283066/2073595 – dano Apr 03 '14 at 15:22
  • looking at the wexpect the code would be: `child = wexpect.spawn('/bin/bash -c "echo ba; sleep 1; echo bu"', logfile=sys.stdout); x=child.expect(wexpect.EOF); child.close()` How would I go about implementing the wget code from above into it? – user1067332 Apr 03 '14 at 15:42
  • Well, you copy and paste the wget command into the spawn call. What is not clear about that? – Emilia Bopp Apr 03 '14 at 15:48
  • `child = wexpect.spawn('wget -r --accept "*.exe,*.dll,*.zip,*.msi,*.rar,*.iso" ftp://ftp.bartylak.com/ -P e:\e', shell=True, '/bin/bash -c "echo ba; sleep 1; echo bu"', logfile=sys.stdout); x=child.expect(wexpect.EOF); child.close()` This gives me an error, I have tried researching but couldnt find any information. – user1067332 Apr 03 '14 at 16:58
  • No, like this: `wexpect.spawn('/bin/bash -c ""', ...)` – dano Apr 03 '14 at 17:13
  • Sorry for the trouble but im still getting errors `child = wexpect.spawn('/bin/bash -c "wget -r --accept "*.exe,*.dll,*.zip,*.msi,*.rar,*.iso" ftp://ftp.bartylak.com/ -P e:\e"', "echo ba; sleep 1; echo bu", logfile=sys.stdout); x=child.expect(wexpect.EOF); child.before child.close()' – user1067332 Apr 03 '14 at 17:31