Multi-line strings with formatting

gburdell1 at gmail.com gburdell1 at gmail.com
Fri Mar 23 12:54:45 EDT 2007


When constructing a particularly long and complicated command to be
sent to the shell, I usually do something like this, to make the
command as easy as possible to follow:

commands.getoutput(
    'mycommand -S %d -T %d ' % (s_switch, t_switch)   +
    '-f1 %s -f2 %s '         % (filename1, filename2) +
    '> %s'                   % (log_filename)
    )

Can anyone suggest a better way to construct the command, especially
without the "+" sign at the end of each line (except the last) ? If I
take out the "+", then I need to move all the variables to the end, as
so:

commands.getoutput(
    'mycommand -S %d -T %d '
    '-f1 %s -f2 %s '
    '> %s'
    % (s_switch, t_switch, filename1, filename2, log_filename)
    )

or:

commands.getoutput(
    '''mycommand -S %d -T %d \
    -f1 %s -f2 %s \
    > %s'''
    % (s_switch, t_switch, filename1, filename2, log_filename)
    )

but having the variables line-by-line as in the first example is so
much easier to edit, is it not?




More information about the Python-list mailing list