Avoid newline at the end

F

Florian Lindner

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
 
P

Paul Hankin

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'))
 
S

Steven D'Aprano

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.
 
P

Paul Rubin

Florian Lindner said:
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'))
 
F

Florian Lindner

Steven said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top