我原来的 2.7 代码在这里:
myFile = open(prjFile, 'w+')
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=hostName, hostNameshort=hostNameshort, prjDir=prjDir, prjCode=prjCode))
myFile.close()
我试图通过这样做使其与 2.4 兼容:
myFile = open(prjFile, 'w+')
myFile.write("""<VirtualHost 192.168.75.100:80>
ServerName www.%(hostName)s
ServerAlias %(hostNameshort).* www.%(hostNameshort)s.*
DocumentRoot %(prjDir)s/html
CustomLog \\|/usr/sbin/cronolog /var/log/httpd/class/prjCode}/\%(hostName)s.log.%Y%m%d\" urchin"
</VirtualHost>""" % ('hostName', 'hostNameshort', 'prjDir', 'prjCode'))
myFile.close()
但我的错误是
Traceback (most recent call last):
File "testfunction.py", line 20, in <module>
</VirtualHost>""" % ('hostName', 'hostNameshort', 'prjDir', 'prjCode'))
TypeError: format requires a mapping
我在这里搜索了答案,但我尝试的任何方法似乎都不起作用。我在这里做错了什么?