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

Victor Hooi victorhooi at gmail.com
Mon Nov 4 17:33:51 EST 2013


Hi,

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

    LOG_FILENAME = 'my_something_{}.log'.format(datetime.now().strftime('%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
    /opt/my_project_venv/bin/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 "my_script.py", line 25, in <module>
        LOG_FILENAME = 'my_something_{}.log'.format(datetime.now().strftime('%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(datetime.now().strftime('%Y-%d-%m_%H.%M.%S'))
    >>> print(LOG_FILENAME)
    my_project_2013-05-11_09.29.47.log

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



Cheers,
Victor



More information about the Python-list mailing list