Search code examples
pythonlinuxbashwebserver

How can I check if a javascript file exist without downloading it in bash or python


How can I check if a javascript file exist without downloading it in bash or python


Solution

  • You can use python urllib module to check file existence in a given URL. Here's a sample code. The results are saved inside the "results" dictionary.

    from urllib.request import urlopen
    urls = ['https://www.codecademy.com/cdn-cgi/challenge- platform/h/g/scripts/alpha/invisible.js','https://www.codecademy.com/idontexist.js']
    results={}
    for url in urls:
        try:
            ret = urlopen(url)
            if ret.code == 200:
                state="Exists"
                print(url+' : {}'.format(state))
        except:
            state="Not exists"
            print(url+' : {}'.format(state))
    
        results[url]=state