-1

I have data base of file. I'm writing a program to ask the user to input file name and using that input to find the file, download it,make a folder locally and save the file..which module in Python should be used?

Joe
  • 6,758
  • 2
  • 26
  • 47
Sameer
  • 19

2 Answers2

2

Can be as small as this:

import requests

my_filename = input('Please enter a filename:')
my_url = 'http://www.somedomain/'
r = requests.get(my_url + my_filename, allow_redirects=True)
with open(my_filename, 'wb') as fh:
    fh.write(r.content)
Joe
  • 6,758
  • 2
  • 26
  • 47
1

Well, do you have the database online? If so I would suggest you the requests module, very pythonic and fast. Another great module based on requests is robobrowser.

Eventually, you may need beautiful soup to parse the HTML or XML data.

I would avoid using selenium because it's designed for web-testing, it needs a browser and its webdriver and it's pretty slow. It doesn't fit your needs at all.

Finally, to interact with the database I'd use sqlite3

Here a sample:

from requests import Session
import os

filename = input()

with Session() as session:
    url = f'http://www.domain.example/{filename}'
    try:
        response = session.get(url)
    except requests.exceptions.ConnectionError:
        print('File not existing')
    download_path = f'C:\\Users\\{os.getlogin()}\\Downloads\\your application'
    os.makedirs(dowload_path, exist_ok=True)
    with open(os.path.join(download_path, filename), mode='wb') as dbfile:
        dbfile.write(response.content)

However, you should read how to ask a good question.

Federico Rubbi
  • 714
  • 3
  • 16