Based on what you are trying to do, you seem to be attempting to combine using a comprehension and calling the write on the file. The syntax error you are getting is because of the clear misuse of what you are trying to do. What you are actually trying to do, is probably something like this:
[f.write(x) for x in port]
However, this is wrong as well. You are using a list comprehension for its side effects, which is a very bad use of a list comprehension. You are creating a useless list just for the sake of trying to save lines of code.
Instead, as mentioned in the other answers, iterate, and call write:
for port in ports:
f.write("{}\n".format(ports))
Extra bonus, to make your code more robust, is to make use of a context manager for your file manager, so that the file gets closed after you use it. Currently, you should be explicitly be calling an f.close() on your file. So, you can do:
with open("test.html", "w") as f:
for port in ports:
f.write("{}\n".format(port))
portsinto your html file?