2

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.

11
  • I don't see anything wrong with the string. Are all your parenthesis closed on the previous line? Commented May 31, 2013 at 17:32
  • In your traceback it looks like you removed the error type. Can you include this so we can better diagnose the problem? Commented May 31, 2013 at 17:33
  • The problem might be on some the previous line as mgilson mentioned. You chould also check that all the ' are closed as well. Commented May 31, 2013 at 17:34
  • SyntaxError: invalid syntax Commented May 31, 2013 at 17:34
  • Seems to be working in both Python 2.7 and 3. Commented May 31, 2013 at 17:35

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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))

1 Comment

This works wonderfully, and I am going to use this triply quoted system with .format more often. Very nice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.