开发者

inserting character in file , jython

开发者 https://www.devze.com 2023-02-16 08:09 出处:网络
I have written a simple program where to read the first 4 characters and get the integer of it and read those many character and write xxxx after it . Although the program is working the only issues i

I have written a simple program where to read the first 4 characters and get the integer of it and read those many character and write xxxx after it . Although the program is working the only issues instead of inserting the 开发者_如何学运维character , its replacing.

file = open('C:/40_60.txt','r+')
i=0
while 1:
    char = int(file.read(4))
    if not char: break
    print file.read(char)
    file.write('xxxx')

print 'done'
file.close()

I am having issue with writing data . considering this is my sample data 00146456135451354500107589030015001555854640020 and expected output is 001464561354513545xxxx00107589030015001555854640020

but actually my above program is giving me this output

001464561354513545xxxx7589030015001555854640020 ie. xxxx overwrites 0010.

Please suggest.


Files do not support an "insert"-operation. To get the effect you want, you need to rewrite the whole file. In your case, open a new file for writing; output everything you read and in addition, output your 'xxxx'.

0

精彩评论

暂无评论...
验证码 换一张
取 消