0

I have a config file that i would like to add a space (' ') to the end of every line in the file

File example:

#xxx configuration
IPaddr = 1.1.1.1<add a space here>
host = a.b.c.d<add a space here>
usrname = aaa<add a space here>
dbSID = xxx<add a space here>

i already have the number of lines in the file (using len) so i know how much time to repeat the loop of adding the space string. i also know how to open a file for reading and writing.

Thank you all.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Asfbar
  • 259
  • 6
  • 21
  • since what i don't know is how to get to the end of the lines and add/write a sting to it – Asfbar Jun 11 '15 at 09:36
  • 2
    What exactly are you trying to achieve by appending this whitespace at the end of the line? – Lix Jun 11 '15 at 09:36
  • 1
    Which operating system are you using. If you are using some unix based systems I would recommend you so use some tools like sed or awk which can do that very easily for you. – nu11p01n73R Jun 11 '15 at 09:38

3 Answers3

3

You can use fileinput with inplace=True to update the original file:

import fileinput
import sys
for line in fileinput.input("in.txt",inplace=True):
    sys.stdout.write("{} \n".format(line.rstrip()))

Or use a tempfile.NamedTemporaryFile with shutil.move:

from tempfile import NamedTemporaryFile
from shutil import move

with open("in.txt") as f, NamedTemporaryFile("w",dir=".", delete=False) as temp:   
    for line in f:
       temp.write("{} \n".format(line.rstrip()))
move(temp.name,"in.txt")
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

You don't need to know the length of your file. In python files are iterators that yield lines. No need for c-style for-loops.

So something like this should work for you (python3 or from __future__ import print_function):

with open('file.in') as infile, open('file.out', 'w') as outfile:
    for line in infile:
        print(line.strip() + ' ', file=outfile)
zefciu
  • 1,967
  • 2
  • 17
  • 39
  • {with open('x.txt') as infile, open('x.txt', 'w') as outfile: ^ SyntaxError: invalid syntax{ – Asfbar Jun 11 '15 at 11:05
  • What version of Python are you using? – zefciu Jun 12 '15 at 07:47
  • If you are forced to use python 2.6, you can use ``contextlib.nested`` http://stackoverflow.com/questions/3024925/python-create-a-with-block-on-several-context-managers so the above would look like: ``with nested(open('file.in'), open('file.out', 'w')) as (infile, outfile):`` – zefciu Jun 15 '15 at 12:46
0

You can do this (on the same file) by opening the file for reading with "r" flag,

Then, for each row in the file, add a space before the new line character ("\n"),

And finally, open the file for writing with "w' flag, and write the updated content with writelines() function.

with open('config.txt', 'r') as source:
    lines = [line.replace("\n"," \n") for line in source.readlines()]
with open("config.txt", "w") as target:
    target.writelines(lines)

Please note, I assume that the file is a UNIX file so it does not contain "\r" character before the "\n", if it is not the case, line number 2 should be:

    lines = [line.replace("\r\n"," \r\n") for line in source.readlines()]
Yogev Neumann
  • 2,099
  • 2
  • 13
  • 24
  • I believe that the negative score for my answer is unfair – Yogev Neumann Mar 30 '19 at 17:09
  • I didn't vote, but answers only showing code without any explanation are considered low quality on Stack Overflow and are sometimes downvoted by users for that reason. You should probably edit your answer and add some text explaining your solution. – Modus Tollens Mar 30 '19 at 17:15
  • Thanks @ModusTollens for your comment, I edited my answer and added an explanation – Yogev Neumann Mar 31 '19 at 07:51