ValueError: zero length field name in format - Running under Python 2.7.3?

Victor Hooi victorhooi at
Mon Nov 4 23:33:51 CET 2013


I have a Python script that's using a format string without positional specifiers. I.e.:

    LOG_FILENAME = 'my_something_{}.log'.format('%Y-%d-%m_%H.%M.%S'))

I'm running this from within a virtualenv, running under Python 2.7.3.

    $ python -V
    Python 2.7.3
    $ which python

The first line of the script is:

    #!/usr/bin/env python

However, when I run this line, I get the following error:

    Traceback (most recent call last):
      File "", line 25, in <module>
        LOG_FILENAME = 'my_something_{}.log'.format('%Y-%d-%m_%H.%M.%S'))
    ValueError: zero length field name in format

The weird thing, when I start a Python REPL and run that line interactively, it works fine:

    $ python
    Python 2.7.3 (default, Jan  7 2013, 11:52:52)
    [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from datetime import datetime
    >>> LOG_FILENAME = 'my_project_{}.log'.format('%Y-%d-%m_%H.%M.%S'))
    >>> print(LOG_FILENAME)

My understanding was that in Python 2.7/3.1, you could omit the positional specifiers in a format string.


More information about the Python-list mailing list