praa
praa

Reputation: 147

Save .txt url in a folder

I am practicing python and I have a list that has a a url and I need to go to the url and save the page content in save_folder

list_name = ['www.xyz.com/abc/sample1.txt','www.xyz.com/abc/sample2.txt','www.xyz.com/abc/sample44.txt']

for i in list_name:
  http = urllib3.PoolManager()
    r = http.request('get', i)
    with open('save_folder/' + i,'w') as f:
        f.write(r.data)
        f.close

When I run the above code I get an error No such file or directory: 'save_folder/www.xyz.com/abc/sample1.txt

The goal is to read the three items on the list and use urllib3 to go to each page on the list and save the each page in save_folder with the filename as sample1.txt,sample2.txt,sample44.txt

Upvotes: 2

Views: 79

Answers (1)

Arun Sg
Arun Sg

Reputation: 51

use only the file name and make sure the urls are accessible.

for i in list_name:
   http = urllib3.PoolManager()
   r = http.request('get', i)
   fname = i.split("/")[-1]
   with open('./save_folder/' + fname,'wb') as f:
     f.write(r.data)
     f.close

Upvotes: 2

Related Questions