Проверьте, существует ли файл URL из текстового файла. - PullRequest
0 голосов
/ 31 мая 2019

Я пытаюсь сделать следующее:

1 - open a text file containing a list with URLs (http://example.com). <br>
2 - read the text file and check if the path existe. <br>
3 - write the results back in another text file.

Я попробовал следующий код:

    import urllib2
    file = open('file.txt', 'r')

    search = urllib2.urlopen(file + "/js/tools.js")

    if search.code == 200:
        print "Exists!"

Я ценю любую предоставленную помощь.

1 Ответ

0 голосов
/ 31 мая 2019

учитывая, что у вас есть файл Filename, поскольку ссылки хранятся построчно

import requests
with open(filename,'r') as file, open(filename2,'w+') as file2:
    for url in file.readlines():
        check = requests.get(url)
        if check.ok:
            file2.write(url)
...