Home > Mobile >  How can I get Python to use 1 line at a time for checking a URL?
How can I get Python to use 1 line at a time for checking a URL?

Time:10-14

So I'm trying to create a python script to check data on client's websites for errors, I basically want to use a txt file with the necessary end of URL's and have the script test one line at a time.

This is the snippet from my script:

with open('numbers.txt') as numbers:
    for index, line in enumerate(numbers)

def urlnumber():
    number = numbers
    url = "http://www.url.com/"   number
    print ("Processing: " url)
    result = checkErr(url)
    print(result)

For reference numbers.txt contains:

One
Two
Three
Four
Five

And I'm trying to make the script check "url.com/one" then "url.com/two" and so on.

If this question has been asked before, please point me in that direction, I have had a look at some similar questions, but the answers did not help me!

Thanks in advance for any help!

CodePudding user response:

with open('numbers.txt') as f:
    # Read file and split them into a list called `numbers`
    numbers = f.read().splitlines()

for number in numbers:
    url = "http://www.url.com/"   number
    print ("Processing: " url)
    result = checkErr(url)
    print(result)

I guess this does a cleaner job. I'd recommend you clean up the code inside the for loop though.

CodePudding user response:

Python is a dynamically strongly typed language. So it won't convert an integer to a string when you try to concatenate them.

You have to either use string interpolation or explicitly convert it to a string.

for i in range(0, 10):
       url = "http://www.url.com/"   str(x)
  • Related