-1

I've tried many different things to achieve this, simply I want this:

import requests
path1 = 'D:\test\Files\/results.txt'
lines1 = open(path1).readlines()
ctr = 0
for i in lines1:
    try:
        r = requests.get(i)
        if r.status_code != 200:
            ctr += 1
            print(i, " Status Code: ", r.status_code)
except requests.ConnectionError:
    ctr += 1
    print(i, " Failed to connect")
print("Counter", ctr)

to output like this:

URL Status Code: xyz

But instead I'm getting:

URL
Status Code: xyz

So, what's the best way to print out something in the same line with Python?

OJFord
  • 10,522
  • 8
  • 64
  • 98
Leustad
  • 143
  • 3
  • 13

1 Answers1

4

i is a line. Just strip it to remove any possible newline at the end, before printing it:

 print(i.rstrip(), " Status Code: ", r.status_code)
JuniorCompressor
  • 19,631
  • 4
  • 30
  • 57