-1

This program downloads files from links provided in a urls.txt file, and it assumes that the python script and urls.txt are in the same folder. How can I change the program to ask the user to provide a path to store the files and update the urlretrieve line? And how could I make the program print the current link being downloaded and the name of the file being downloaded?

import os.path
import urllib.request

links = open('urls.txt', 'r')
for link in links:

    # Get one line of text (e.g. http://server/files/grades.doc),
    # then get the filename from the end of the URL
    link = link.strip()
    filename = link.rsplit('/', 1)[-1]

    # Does this file exist in this folder? If not, download it
    if not (os.path.isfile(filename)):
        print('Downloading: ' + filename)
        try:
            urllib.request.urlretrieve(link, filename)
            print("File size was", os.path.getsize(filename))
        except Exception as inst:
            print(inst)
            print('  Encountered unknown error. Continuing.')

    # File exists; don't download
    else:
        print("This file exists already.")

# End of program
print("Finished downloading.")

The requirements are: enter image description here

1 Answers1

0

There are many ways to prompt the user. One will be to ask via the command line at the start and then convert the string input to the path. For example

from pathlib import Path
out_path = input('Enter the path to save the files')
path = Path(out_path)

or

import os
out_path = input('Enter the path to save the files')
path = os.path.normpath(out_path)

Read more about str to path in this answer

Another method will be using a GUI like Tkinter. For example

from tkinter.filedialog import askdirectory
path = askdirectory()

After getting the path one can add that to the filename as follows

filename = os.path.join(path,filename)

Printing progress can be done with

print(f'Now downloading {link} and saving as {filename}')

Edit: Incorrect variable name in os.path.normpath

unityJarvis
  • 353
  • 1
  • 9
  • i tried putting import os out_path = input('Enter the path to save the files') path = os.path.normpath(p) It came with the error: import os out_path = input('Enter the path to save the files') path = os.path.normpath(p) – Jekskie Oct 31 '21 at 09:37
  • Sorry, my bad, it's `os.path.normpath(out_path)`, edited the answer as well. Thank you – unityJarvis Oct 31 '21 at 10:26
  • i tried the code after adding what you gave and i think its running, but theres an invalid argument [ERRNO22]|| Enter the path to save the files C:\Users\User\Desktop\PTK Downloading: C:\Users\User\Desktop\PTK\10meg.test [Errno 22] Invalid argument: ' C:\\Users\\User\\Desktop\\PTK\\10meg.test' Encountered unknown error. Continuing. Downloading: C:\Users\User\Desktop\PTK\50meg.test [Errno 22] Invalid argument: ' C:\\Users\\User\\Desktop\\PTK\\50meg.test' Encountered unknown error. Continuing. Finished downloading. Process finished with exit code 0 – Jekskie Oct 31 '21 at 10:44
  • Please check and find out which line is failing here, i.e., urlretrieve or os.path.getsize. – unityJarvis Oct 31 '21 at 11:18