-1

Need Help

Get input for where the file is located & tell the new directory to save downloaded URLs to filepath variable assumes the text document with links is in the location of the python script - while this works, I would also like this to change to direct.

    import os.path
    import urllib.request

    # Get one line of text (e.g. C:\user\user\path\directory)

    filepath = input('please input the url file path: ')
    links = open('links.txt', 'r')
    newpath = input('Where would you like to store the file? ')
    for link in links:`
        # 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 + " to " + newpath)
        try:
            urllib.request.urlretrieve(link, newpath + 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."

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
archiemax
  • 1
  • 2
  • Please explain the problem and tell us what you actually want to do. Your current description makes very little sense as to what you want to do. – AzyCrw4282 Mar 26 '20 at 23:13
  • I'm trying to create a script that takes either a .txt or .xml document ( starting with .txt first as to not over complicate it.) user input where the (.txt) file is located - outside of my python script folder, and direct the program to save the output of the downloaded items from the list to a new folder that relys on user input. – archiemax Mar 26 '20 at 23:42
  • Ok, doable! What is the issue with the current code, how does it behave? – AzyCrw4282 Mar 26 '20 at 23:44
  • "C:\Users\Archie\PycharmProjects\List download test\venv\Scripts\python.exe" please input the url file path: C:\Users\Archie\Desktop\list Where would you like to store the file? C:\Users\Archie\Desktop\outputDL This file exists already. This file exists already. This file exists already. Finished downloading. Process finished with exit code 0 the files download within the python script folder fine and give me this output ( they are currently in there ) the newpath variable doesn't seem to connect and I'm lost as to how to fix it. – archiemax Mar 26 '20 at 23:56

1 Answers1

0

If your error is concerning why the newpath doesn't seem to connect to it. This is because I believe, in this line of code urllib.request.urlretrieve(link, newpath + filename), the link param doesn't represent anything. You may try and print that value and see what it prints.

Note that link in the for loop is a local variable, and anything saved in there will not be accessible anywhere else in the code.

for link in links:`
   # then get the filename from the end of the URL
   link = link.strip()
   filename = link.rsplit('/', 1)[-1]

Try defining a global variable named link outside the loop so that the value saved in the loop can be preserved to be used everywhere in the code. Like so,

filepath = input('please input the url file path: ')
links = open('links.txt', 'r')
newpath = input('Where would you like to store the file? ')
link = ""
AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • "C:\Users\Archie\PycharmProjects\List download test\venv\Scripts\python.exe" please input the url file path: C:\Users\Archie\Desktop\list Where would you like to store the file? C:\Users\Archie\Desktop\outputDL Downloading: aardvark_014.jpg to C:\Users\Archie\Desktop\outputDL [WinError 2] The system cannot find the file specified: 'aardvark_01.jpg' Encountered unknown error. Continuing. Finished downloading. this is the output I get when I direct it to C:\outputDL, if i leave the first input blank with the list inside the python script folder it downloads correctly. – archiemax Mar 27 '20 at 00:40
  • Can you replace `print ('Downloading: ' + filename + " to " + newpath)` with this `print ('Downloading: ' + filename + " to " + newpath + " "+ link)` and show me the output plz – AzyCrw4282 Mar 27 '20 at 00:42
  • "C:\Users\Archie\PycharmProjects\List download test\venv\Scripts\python.exe" please input the url file path: C:\Users\Archie\Desktop\list Where would you like to store the file? C:\Users\Archie\Desktop\outputDL Downloading: aardvark_thumb.jpg to C:\Users\Archie\Desktop\outputDL https://www.nationalgeographic.com/content/dam/animals/thumbs/rights-exempt/mammals/a/aardvark_thumb.JPG [WinError 2] The system cannot find the file specified: 'aardvark_thumb.jpg' Encountered unknown error. Continuing. – archiemax Mar 27 '20 at 00:49
  • Traceback (most recent call last): File "C:/Users/Archie/PycharmProjects/List download test/Downloadlist.py", line 5, in links = open('url.txt', 'r') FileNotFoundError: [Errno 2] No such file or directory: 'url.txt' # if I change the name of the .txt that's not held in the python library root I get this error this may be where I'm missing critical code?# – archiemax Mar 27 '20 at 00:59
  • Can you try doing this `urllib.request.urlretrieve("https://www.nationalgeographic.com/content/dam/animals/thumbs/rights-exempt/mammals/a/aardvark_thumb.JPG")` and see if it works? It may be that its expecting a full url and you're not supplying that. If problems persists, see [here](https://stackoverflow.com/questions/7243750/download-file-from-web-in-python-3) for alternatives – AzyCrw4282 Mar 27 '20 at 01:08
  • the problem still persists, I believe the problem is it is not wanting to direct outside of the C:\Users\Archie\PycharmProjects\List download test library. thank you for your help, it was greatly appreciated. – archiemax Mar 27 '20 at 01:14