Forums

Create folder by book id and download file from batch file

I'm using Python 3.6.5 on Windows 10

First, I'll create folder by book id, it works :

And then, Download file from batch file, one URL per line in file. URL:

  • http://url.com/page-1.jpg
  • http://url.com/a.mp3
  • http://url.com/b.mp3

Here are code and works :

import os
import wget

book_id = ["5151","5152","5153"]

for id in book_id:
   directory ="new/"+ str(id)
   if not os.path.exists(directory):
        os.makedirs(directory)


   with open ("%s_url.txt" % id, encoding='utf-8', mode = 'r') as f:
        for url in f.readlines():
            filename = wget.download(url.strip())
            print (filename)

5151_url.txt file is not in 5151 folder. I need to move download file into 515 folder.

I know it could be use `wget.download(url, fullfilename), and fullfilename = os.path.join(directory, filename)

filename = page-1.jpg ..etc.`

But this will change file name, I don't wanna change file name. How to ?

My code need to fix again,

Thank you so much for helping.

Hi there -- this is the forum for PythonAnywhere, an online development/hosting environment for Python. We can't really help with general programming problems.