title: Download file from URL and save it in a folder Python
body: <p>I've a lot of URL with file types <code>.docx</code> and <code>.pdf</code> I want to run a python script that downloads them from the URL and saves it in a folder. Here is what I've done for a single file I'll add them to a for loop:</p>
  
  <pre><code>response = requests.get('http://wbesite.com/Motivation-Letter.docx')
  with open("my_file.docx", 'wb') as f:
      f.write(response.content)
  </code></pre>
  
  <p>but the <code>my_file.docx</code> that it is saving is only 266 bytes and is corrupt but the URL is fine.</p>
  
  <h3>UPDATE:</h3>
  
  <p>Added this code and it works but I want to save it in a new folder.</p>
  
  <pre><code>import os
  import shutil
  import requests
  
  def download_file(url, folder_name):
      local_filename = url.split('/')[-1]
      path = os.path.join("/{}/{}".format(folder_name, local_filename))
      with requests.get(url, stream=True) as r:
          with open(path, 'wb') as f:
              shutil.copyfileobj(r.raw, f)
  
      return local_filename
  </code></pre>