Reputation: 35
I have a file that has multiple urls and list with url paths. I am trying to concatenate or join them together with the url paths in the list. I am having a lot of trouble trying to get this to work.
file has urls like this
foobar.com
foobar.com.tk
foobar.org
list1 = ['/foobar.php','/foobar.html','/foobar.php']
with open('file1.txt') as f:
Nlist = [line.strip() for line in f]
I don't know if it matters or not but the file with urls doesn't have the http:// header and when I try to join the urls from the list with the paths I keep getting an error or the code is all bunched up...How do I join the urls from the file with the paths from the list?
Upvotes: 1
Views: 168
Reputation: 26335
You could get every url combination with itertools.product
:
from itertools import product
from pprint import pprint
list1 = ["/foobar.php", "/foobar.html", "/foobar.php"]
with open("file1.txt") as f:
pprint(
set(
"https://%s%s" % (root, path)
for root, path in product(map(str.strip, f), list1)
)
)
Urls:
{'https://foobar.com.tk/foobar.html',
'https://foobar.com.tk/foobar.php',
'https://foobar.com/foobar.html',
'https://foobar.com/foobar.php',
'https://foobar.org/foobar.html',
'https://foobar.org/foobar.php'}
Note: You can use set()
here to remove duplicate urls from the result.
Upvotes: 1
Reputation: 2710
list1 = ['/foobar.php','/foobar.html','/foobar.php']
with open('file1.txt') as f:
Nlist = [line.strip() for line in f]
for i in range(len(Nlist)):
pth = 'https://' + Nlist[i] + list1[i]
print(pth)
os.path.join()
doesn't seem to like all of the dot whatevers, so it seems that you have to resort to your own concatenation.
Upvotes: 1