Download files from a list if not already downloaded

后端 未结 3 478
离开以前
离开以前 2020-12-31 15:02

I can do this in c#, and the code is pretty long.

Would be cool if someone can show me how this would be done via python.

Pseudo code is:

u         


        
3条回答
  •  清酒与你
    2020-12-31 15:48

    Here is a slightly modified version of WoLpH's script for Python 3.3.

    #!/usr/bin/python3.3
    import os.path
    import urllib.request
    
    links = open('links.txt', 'r')
    for link in links:
        link = link.strip()
        name = link.rsplit('/', 1)[-1]
        filename = os.path.join('downloads', name)
    
        if not os.path.isfile(filename):
            print('Downloading: ' + filename)
            try:
                urllib.request.urlretrieve(link, filename)
            except Exception as inst:
                print(inst)
                print('  Encountered unknown error. Continuing.')
    

提交回复
热议问题