1

could someone help me please? I have an output.txt file (example file) which looks like this:

output.txt:

account.netflix.com
prod.netflix.com
netflix.com
https://google.com

and I’m trying to put in a file alive.txt just only the existing url with this code

import socket

with open("output.txt", 'r') as f:
    for url in f:
        try:
            addr1 = socket.gethostbyname(url)
            print(addr1 + " is a valid url")

            f = open("alive.txt", "a")
            f.write("\n" + addr1)
            f.close()

        except:
            print("not valid")

f.close()

what am I doing wrong?

3
  • are you tried to check if it's valid url and if yes write to file ? Commented Apr 30, 2020 at 7:30
  • can you try my answer ? i´ve updated :) Commented Apr 30, 2020 at 7:41
  • I suggest you to use regex Commented Apr 30, 2020 at 7:57

2 Answers 2

2

The problem is when you read the url from the file. When you read the url in url variable, it has an endling \n newline character with it. You have to remove this and then use the url.

Replace this line:

addr1 = socket.gethostbyname(url)

with this one:

addr1 = socket.gethostbyname(url.rstrip())

Moreover, I think you are not aware of python that much, as your code has various things that needs correction.

  1. You have two file references with same name f. Don't do this, it will lead to some strange behavior.
  2. You are opening the file again and again in for-loop, it's not pythonic. Just open the file once, and when the task ends, close the file.
  3. Using with to open the file you don't have to close the file later on. with is just for this purpose so, that there is no need to close the file.

You can check given code for above mentioned points.

import socket

with open("test.txt", 'r') as in_file, open("alive.txt", "a") as out_file :
    for url in in_file:
        try:
            addr1 = socket.gethostbyname(url.rstrip())
            print(addr1 + " is a valid url")
            out_file.write("\n" + addr1)
        except:
            print("not valid")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the explanation, it helps me a lot. And yes, I have started with Python one week ago... :)
Glad to help, If it helps never forget to give a upvote :)
0

Maybe you can try changing the order of your functions :

import socket

f = open("alive.txt", "w")
with open("output.txt") as fp:
    line = fp.readline()

    while line:
        try:
            clean_line = line.strip()
            addr1 = socket.gethostbyname(clean_line)
            print("valid url : " + addr1 + " from " + clean_line)
            f.write(addr1 + "\n")

        except:
            print("not valid: " +addr1 + " from " + clean_line)

        line = fp.readline()

f.close()

2 Comments

Nice, thank you for you help! this was exactly what I needed. :) I just replace the write part instead of addr1 I added clean_line
Glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.