Reputation:
SOLVED
I have this string:
' ServerAlias {hostNameshort}.* www.{hostNameshort}.*'.format(hostNameshort=hostNameshort)
But it keeps giving me a syntax error. The line is supposed to be this bash equivalent:
echo " ServerAlias ${hostOnly}.* www.${hostOnly}.*" >> $prjFile
Mind you the first string is a part of a myFile.write function but that isn't the issue, I can't even get the string to make enough sense for it to let me run the program.
Traceback:
File "tomahawk_alpha.py", line 89
' ServerAlias {hostNameshort}.* www.{hostNameshort}.*'.format(hostNameshort=hostNameshort)
^
But no matter how I change that '
symbol it doesn't seem to work. What am I doing wrong?
In response to @mgilson:
myFile = open(prjFile, 'w+')
myFile.write("<VirtualHost 192.168.75.100:80>"
" ServerName www.{hostName}".format(hostName=hostName)
' ServerAlias {hostNameshort}.* www.{hostNameshort}.*'.format(hostNameshort=hostNameshort)
" DocumentRoot ", prjDir, "/html"
' CustomLog "\"|/usr/sbin/cronolog /var/log/httpd/class/',prjCode,'/\{hostName}.log.%Y%m%d\" urchin"'.format(hostName=hostName)
"</VirtualHost>")
myFile.close()
I had each line in it's own myFile.write line, but it only produced the first line and then quit. So I assumed calling it once and spacing it like that would create intended result.
Upvotes: 2
Views: 407
Reputation: 309929
Automatic string concatenation only works with string literals:
"foo" "bar"
results in "foobar"
But, the following won't work:
("{}".format("foo")
"bar")
which is analogous to what you are doing. The parser sees something like this:
"{}".format("foo") "bar"
(because it joins lines where there are unterminated parenthesis) and that is clearly not valid syntax. To fix it, you need to concatenate the strings explicitly. e.g:
("{}".format("foo") +
"bar")
Or use string formatting on the entire string, not just one piece of it at a time.
Upvotes: 4
Reputation: 159905
You have a couple of syntax errors. However, you may want to look at using a triply quoted strings instead - much easier to modify in the long run:
myFile.write("""<VirtualHost 192.168.75.100:80>
ServerName www.{hostName}
ServerAlias {hostNameshort}.* www.{hostNameshort}.*
DocumentRoot {prjDir}/html
CustomLog "\"|/usr/sbin/cronolog /var/log/httpd/class/{prjCode}/\{hostName}.log.%Y%m%d\" urchin"
</VirtualHost>""".format(hostName=hn, hostNameshort=hns, prjDir=prjd, prjCode=prjc))
Upvotes: 3