Prasad
Prasad

Reputation: 13

Error closing wget subprocess in Python

I am downloading a file using wget in Python using the code below:

p1 = subprocess.Popen(['wget',
                       '-P', 
                       'path/to/folder','http://www.ncbi.nlm.nih.gov/Traces/wgs/?download=ACHI01.1.fsa_nt.gz'],
                       stdout=subprocess.PIPE)

p1.studout.close()

The file gets downloaded and saved correctly in the given folder but the process keeps running. I tried p1.kills() but that doesn't work either. Please advise.

Thanks!

Upvotes: 1

Views: 1075

Answers (2)

Stefan Gruenwald
Stefan Gruenwald

Reputation: 2640

Use subprocess.call

import subprocess 

subprocess.call (['wget', '-P', '/', 'http://www.ncbi.nlm.nih.gov/Traces/wgs/?download=ACHI01.1.fsa_nt.gz'])

Upvotes: 1

jcoppens
jcoppens

Reputation: 5440

A call to wait() is missing after the Popen, to make the main process wait till the child process is terminated.. The following code seems to work fine:

import subprocess as sp

def main(args):
p1 = sp.Popen(['wget', '-P path',
               'http://www.ncbi.nlm.nih.gov/Traces/wgs/?download=ACHI01.1.fsa_nt.gz'],
               stdout = sp.PIPE)
p1.wait()
return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

(You can also group commandline parameters with their values, if they have any).

Upvotes: 0

Related Questions