title: Python download a file
body: <p>I'm unsure how to do this. One way is:</p>
  
  <pre><code>import urllib.request;
  urllib.request.urlretrieve('www.example.com/file.tar', 'file.tar')
  </code></pre>
  
  <p>Another way would be:</p>
  
  <pre><code>import urllib.request;
  
  #Set as appropriate
  userAgent = ....;
  
  req = urllib.request.Request('www.example.com/file.tar', headers={'User-Agent' : userAgent});
  response = urllib.request.urlopen(req);
  
  #Save the file
  f = open('file.tar', 'wb');
  f.write(response.read());
  f.close()
  </code></pre>
  
  <p>I'm not sure which method to use. I'll be downloading many files (with a pattern filename) in a loop. However, I would like to be able to set up a user-agent header. It's not critical but I'd like to.</p>
  
  <p>EDIT: I forgot to mention that I prefer the first method but I don't know how to set the user-agent header with urlretrieve.</p>