How to place file pointer one line up before writing in file using python? -


scenario

there file contains 2 blank lines @ end. when append file, gets written after 2 blank lines (which certain).

but want 1 blank line , remove second blank line. in place of second blank line, appending data should written.

#-------original file  line 1  line 2 [--blank line--]  line 3 line 4 [--blank line--] [--blank line--] 

appending "this line 5" , "this line 6" in above file.

what happening right now!

#-------original file  line 1  line 2 [--blank line--]  line 3 line 4 [--blank line--]  [--blank line--]   line 5 line 6 

what want !

#-------original file  line 1  line 2 [--blank line--]  line 3 line 4 [--blank line--]  #only 1 blank line. second blank line should removed line 5 line 6 

i have researched , came solution moving file pointer. while appending contents file, file pointer may present after second blank line. work if move file pointer 1 line , append "this line 5" , "this line 6" ?

if yes, please assist on how should that. seek() function not seems useful !

any idea other seek() appreciated.

any highly appreciated.

here simple way reads through file line line, restores pointer after second-last:

with open('fname', 'rw') f:     prev = pos = 0     while f.readline():         prev, pos = pos, f.tell()     f.seek(prev)     # use f 

if don't want spend time reading through file, need decide e.g. line-endings support, while here python you.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -