By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

Avoid newline at the end

P: n/a
Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the second
line.
How to do that most elegantly with Python?

Thanks,

Florian
Nov 11 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 11, 10:22 am, Florian Lindner <Florian.Lind...@xgm.dewrote:
Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the second
line.
How to do that most elegantly with Python?
Naively after your code...
logs = logs.rstrip()

But probably, I'd have constructed the list of logs, then used 'join'
to build the string.

logs = []
for row in resultSet:
for name in ('access.log', 'error.log'):
logs += ['/home/%s/%s/log/%s' % (row[1], row[0], name)]
logs = '\n'.join(logs)

Or equivalently using a list comprehension...

logs = '\n'.join('/home/%s/%s/log/%s' % (row[1], row[0], name)
for row in resultSet
for name in ('access.log', 'error.log'))

--
Paul Hankin

Nov 11 '07 #2

P: n/a
On Sun, 11 Nov 2007 11:22:19 +0100, Florian Lindner wrote:
Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the
second line.
That means your log file doesn't end with a newline. That's often not
good, because it can confuse some tools.

Also, appending lots of strings together like that is very inefficient.
How to do that most elegantly with Python?
If you have a small number of rows (say, less than a few tens of
thousands), you can do this:

rows = []
for row in resultSet:
rows.append("/home/%s/%s/log/access.log" % (row[1], row[0]))
rows.append("/home/%s/%s/log/error.log" % (row[1], row[0]))
# note that there are no newlines
logs = '\n'.join(rows) # do it once at the end

But again, when you write text to a file, you should end it with a
newline. It isn't compulsory, but it is best practice.

Alternatively, check out the logging module.

--
Steven.
Nov 11 '07 #3

P: n/a
Florian Lindner <Fl*************@xgm.dewrites:
for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

def logfile_path(name, row):
return "/home/%s/%s/log/%s " % (row[1], row[0], name)

logs = '\n'.join(logfile_path(name, row)
for row in resultSet
for name in ('access.log', 'error.log'))
Nov 11 '07 #4

P: n/a
Steven D'Aprano wrote:
On Sun, 11 Nov 2007 11:22:19 +0100, Florian Lindner wrote:
>Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the
second line.

That means your log file doesn't end with a newline. That's often not
good, because it can confuse some tools.

Also, appending lots of strings together like that is very inefficient.
>How to do that most elegantly with Python?

If you have a small number of rows (say, less than a few tens of
thousands), you can do this:

rows = []
for row in resultSet:
rows.append("/home/%s/%s/log/access.log" % (row[1], row[0]))
rows.append("/home/%s/%s/log/error.log" % (row[1], row[0]))
# note that there are no newlines
logs = '\n'.join(rows) # do it once at the end

But again, when you write text to a file, you should end it with a
newline. It isn't compulsory, but it is best practice.

Alternatively, check out the logging module.
That is not log file it's a config file for logrotate. And the log string
goes into a template therefore the config file ends with a newline. The
problem is that logrotate gets confused by empty lines between logfile path
and config.
The number of lines will always be < 100 and so config will only be
regenerated not often so efficiency is not issue.
Regards,

Florian
Nov 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.