2

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

praa
  • 147
  • 1
  • 8
  • You probably need to set a working directory. Can you attach a picture of your directory? – Sanjit Sarda Jan 03 '20 at 02:23
  • 1
    Does this answer your question? [Is it possible to use "/" in a filename?](https://stackoverflow.com/questions/9847288/is-it-possible-to-use-in-a-filename) – Marat Jan 03 '20 at 02:38

1 Answers1

2

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
Arun Sg
  • 51
  • 6