-3

I have written a script in python. Now, here I have opened existing file and replace its value with another value. Values are replaced but not updated in other file.

with open ("VAdminTool.properties", "r+") as propfile:
   data1=propfile.read().splitlines()
   listin1 = len (data1)
   for line1 in data1:
     line2 = line1.split('=')
     line2[0] = line2[0].strip()
     if line2[0] == 'YPSAddress':
       line2[1] = line1.replace(line2[1],yps_url)
       print line2[1]
       propfile.write(line2[1])
   propfile.close()
kohi
  • 936
  • 2
  • 12
  • 31
  • 1
    I'm not sure if this is necessarily the problem, but because you used "with" to open the file you shouldn't call propfile.close() at the end, the "with" part already does that for you. – Chachmu Aug 19 '15 at 13:21
  • without seeing some input and desired output it is impossible to answer your question – Padraic Cunningham Aug 19 '15 at 13:23

1 Answers1

1

You have to be very careful when modifying a file in-place like that. The modified text has to be exactly the same size as the original text, or you'll make a mess. The following bytes in the file won't magically move if the replacement text is the wrong size.

But without seeing your data it's not possible to tell if that's a problem or not with your code.

However, your code isn't writing the new text in the correct location, and that is a problem. You can't just write the new text at the current location, you need to seek() to the correct position. The code below show two slightly different ways to deal with that. It can be done in a for line in f: loop, but I think it's somewhat cleaner to do it with a simple while True: loop.

#!/usr/bin/env python

""" Inplace file update demo

    Written by PM 2Ring 2015.08.20

    See http://stackoverflow.com/q/32096531/4014959
"""

def create(fname):
    data = 'zero one two three four five six seven eight nine'
    with open(fname, 'w') as f:
        for s in data.split():
            f.write(s + '\n')

def modify0(fname):
    with open(fname, 'r+') as f:
        fpos = 0
        for line in f:
            print repr(line)
            outline = line[:2].upper() + line[2:]
            f.seek(fpos)
            f.write(outline)
            fpos += len(line)
            f.seek(fpos)

def modify1(fname):
    with open(fname, 'r+') as f:
        while True:
            fprev = f.tell()
            line = f.readline()
            if not line:
                break
            print repr(line)
            f.seek(fprev)
            outline = line[:2].upper() + line[2:]
            f.write(outline)

def show(fname):
    with open(fname, 'r') as f:
        for line in f:
            print repr(line)

modify = modify1
fname = 'testfile.txt'

create(fname)
modify(fname)
print '\n' + 20*' - ' + '\n'
show(fname)

output

'zero\n'
'one\n'
'two\n'
'three\n'
'four\n'
'five\n'
'six\n'
'seven\n'
'eight\n'
'nine\n'

 -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - 

'ZEro\n'
'ONe\n'
'TWo\n'
'THree\n'
'FOur\n'
'FIve\n'
'SIx\n'
'SEven\n'
'EIght\n'
'NIne\n'
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182