I have what is hopefully an easy question for you. It involves adding a variable to an "a href" in python. I am generating files with names that will vary depending on the output of other functions. I am trying to just add a link to a web page that allows someone to click and download the zip file.

import os, sys
path = "C:/output/"
file = "bob.zip"
movedZip = (path + file)

print """<h3><a href=movedZip>Download zip file</a></h3>"""

The problem is that on the webpage, the link shows as "localhost/movedZip". I have tried several iterations of this, but nothing seems to allow the link to be variable.

Thank you for your time, in advance.

有帮助吗?

解决方案

This is simple variable substitution, and doesn't really have anything to do with HREFs. You just need to tell Python to insert the value of your variable into your string.

"""<h3><a href="%s">Download zip file</a></h3>""" % movedZip

or

"""<h3><a href="{}">Download zip file</a></h3>""".format(movedZip)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top