Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Sorry for this very basic question. I am new to Python and trying to write a script which can print the URL links. The IP addresses are stored in a file named list.txt. How should I use the variable in the link? Could you please help?

# cat list.txt

192.168.0.1
192.168.0.2
192.168.0.9

script:

import sys
import os

file = open('/home/list.txt', 'r')

for line in file.readlines():
    source = line.strip('\n')
    print source

link = "https://(source)/result”
print link

output:

192.168.0.1
192.168.0.2
192.168.0.9
https://(source)/result

Expected output:

192.168.0.1
192.168.0.2
192.168.0.9
https://192.168.0.1/result
https://192.168.0.2/result
https://192.168.0.9/result
share|improve this question

5 Answers 5

You need to pass the actual variable, you can iterate over the file object so you don't need to use readlines and use with to open your files as it will close them automatically. You also need the print inside the loop if you want to see each line and str.rstrip() will remove any newlines from the end of each line:

with open('/home/list.txt') as f:  
    for ip in f:
        print "https://{0}/result".format(ip.rstrip())

If you want to store all the links use a list comprehension:

with  open('/home/list.txt' as f:
    links = ["https://{0}/result".format(ip.rstrip()) for line in f]

For python 2.6 you have to pass the numeric index of a positional argument, i.e {0} using str.format .

You can also use names to pass to str.format:

with open('/home/list.txt') as f:
    for ip in f:
        print "https://{ip}/result".format(ip=ip.rstrip())
share|improve this answer
    
Thanks Padraic.. tried but getting this error. Traceback (most recent call last): File "script.py", line 7, in <module> links = ["https://{}/result".format(line.rstrip()) for line in f] ValueError: zero length field name in format –  user3331975 13 hours ago
    
@user3331975, try it now, you are using python < 2.7 yes? –  Padraic Cunningham 13 hours ago
    
yes, that helped Padraic –  user3331975 13 hours ago
    
@user3331975, for python > 2.6 you don't need to pass the positional indexes to str.format –  Padraic Cunningham 13 hours ago

Get the link inside the loop, you are not appending data to it, you are assigning to it every time. Use something like this:

file = open('/home/list.txt', 'r')

for line in file.readlines():
    source = line.strip('\n')
    print source
    link = "https://%s/result" %(source)
    print link
share|improve this answer

Try this:

lines = [line.strip('\n') for line in file]

for source in lines:
    print source

for source in lines:
    link = "https://{}/result".format(source)
    print link

The feature you just described is often called string interpolation. In Python, this is called string formatting.

There are two styles of string formatting in Python: the old style and the new style. What I've shown in the example above is the new style, in which we format with a string method named format. While the old style uses the % operator, eg. "https://%s/result" % source

share|improve this answer

Use format specifier for string and also put the link printing section in the for loop only something like this:

import sys
import os
    file = open('/home/list.txt', 'r')
    for line in file.readlines():
        source = line.strip('\n')
        print source
        link = "https://%s/result”%source
        print link
share|improve this answer
import sys
import os

file = open('/home/list.txt', 'r')

for line in file.readlines():
    source = line.strip('\n')
    print source
    link = "https://" + str(source) + "/result”
    print link
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.