Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12

    open file in append mode


    Hi all,

    does anybody know a possiblitiy to open a file in append mode and change the write position in the file?
    e.g. I open a file in append mode:
    Code:
    fh = open("bla.txt", "a")
    Now I want to write a line in the file but not at the end of the file but one or two lines before the end.
    Is this possible?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Norwich, UK
    Posts
    53
    Rep Power
    13
    As far as I know if you open the file in append mode you can only write to the end of the file. To do what you want you must open with 'r+' and then use the file-object's seek() method to set the pointer where you want.

    I haven't actually used Python to do this so I'm basing this on PHP, but from what I can tell they use the same underlying C mechanisms anyway so it should behave the same way.
    Last edited by sacrilege; August 6th, 2003 at 11:34 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    I think sacrilege is right. File iss pretty low-level or basic, and you don't keep anything in buffer when appending, writing or reading.

    It's easy though. Just put everything after the place you intend to write from in a buffer, then write the buffer back to the file after you've written what you want.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi wizard,

    this should do what you wanted.. It's only a very basic idead! and has no error handlind and etc. but it works.

    1. read in the file and split it on every new line
    2. make two lists containing the data up to the line before where you want to append your data.
    3. insert the string write into the fist list using append
    4. open the file for wring and write the contents of both lists joined with a new line.
    5 and your done, you've just appended to a specific place in a file.

    >>>line = 3
    >>>string = 'insert this string'
    >>>file = open('file.txt', 'r').read().split('\n')
    >>>a = file[:line]
    >>>b = file[line:]
    >>>a.append(string)
    >>>open('file.txt', 'w').write('\n'.join(a + b))

    I made this into a function which works roughly the same way.

    Code:
    def sappend(file, string, line = 0):
    	file = open(file, 'r').read().split('\n')
    	a = file[:line]
    	b = file[line:]
    	a.append(string)
    	open(file, 'w').write('\n'.join(a + b))
    		
    sappend('file.txt', 'insert this string'', 3)
    Hope this helps,
    Mark.
    Last edited by netytan; August 6th, 2003 at 03:04 PM.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
    Thank you for your replies.
    I solve it similar the way netytan proposed.
    Tip @netytan:
    Use readlines()instead of read().split('\n').
    -> same result
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Way, how in hell did I miss that .. silly me thats for the info. Anyway glad you solves it.

    Here's an improved version of my function (using your readlines), all i've desided to do is return the value for writing this way you can easily desided where to write it, of course I could also add this option to the function (example two).

    Code:
    def sappend(file, string, line = 0):
    	file = open(file, 'r').readlines()
    	a = file[:line]
    	b = file[line:]
    	a.append(string)
    	return ''.join(a + b)
    		
    open('text.txt', 'w').write(sappend('file.txt', 'baby\n', 3))
    Code:
    def sappend(file, string, line = 0, out = file):
    	file = open(file, 'r').readlines()
    	a = file[:line]
    	b = file[line:]
    	a.append(string)
    	open(out, 'w').write(''.join(a + b))
    		
    sappend('file.txt', 'baby\n', 3, 'new.txt')
    Still no error messages but it lets you pick where you want output to go .. sorry, interesting function lol. Can you post your solusion would be interesting to see.

    Percivall you talked about buffer's which has me a little lost, can you explain what you ment.

    Mark.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
    My outfile is a xml file that looks like:
    Code:
    <root>
      <subelement>
      </subelement>
      <subelement>
      </subelement>
      <subelement>
      </subelement>
      <subelement>
      </subelement>
      ...
    </root>
    I want to add further <subelement> tags after the last subelement tag.
    I solve the problem by reading the file, deleting the last line (that line with </root>) and append the <subelement> and then append the close root tag (</root>).
    I just asked myself if there is nothing like:
    Code:
    >>> mylist = [1,2,3]
    >>> mylist.insert(-1,2.5)
    >>> print mylist
    [1,2,2.5,3]
    I thougt it might be that there is something similar for file handling, but anyway...
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I tested that last night, seeing if there is a way to do this, i'd actialy forgotten about the interst method, Obviouly i need to look harder next time

    I need to do a little work..well quite a lot with XML (if i cant find a nice alternative) when I eventually get onto this project i'm planning. Any links you would recoment for beginner's looking at the XML API?

    Mark.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
    Search google for "xml xsl xpath tutorial".
    The first hits are very well.
    If you have further questions ask me :-)
    Do you have experience with win32com?
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Thank's mate, No i havn't used COM before although I believe there is a section in "Python Programming" relating to it. You also might wana get the "Python Programming on Win32", havn't read it but there should be a good COM intro in there!

    You can read about & order both from Amazon.com

    Have fun,
    Mark.
    Last edited by netytan; August 8th, 2003 at 05:21 PM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Brisbane, Australia
    Posts
    50
    Rep Power
    12
    I'm new to python, (only 2nd day into python world)

    by reading you guys posts, I wrote this insert function

    takes file
    def qInsert(filename, insStr, insPos = 0):
    f=file(filename, 'r')
    head=f.read(insPos)
    tail=f.read()
    f.close()
    file(filename, 'w').write(head+insStr+tail)
    it works fine. just wondering if the performance of manuplating a string is slower than the performance of manuplating string array or not?
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
    where do you have a string array?
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hey,

    I'm not sure what you mean Wizard so i'll just try and answer oni's question.

    The function works yes, as for preformance i can't see any big problem here you then open a file for writing and send the three strings as a one. You should be able do this all with one file object under append (a)..

    From what I can see this is pretty efficent and gives you more controle - allowing you to select the char you want to add from, instead of the line - too much for what we needed here but still useful .

    This is the function I'm currently using for line insertion , one thing I would suggest you add to your function is the out option, this way a user can optionally tell Python to write the new data to a different location.

    Code:
    def sappend(file, string, line = 0, out = file):
    	file = open(file, 'r').readlines()
    	file.insert(line, string)
    	open(out, 'w').writelines(file)
    
    sappend('file.txt', 'baby\n', 3, 'new.txt')
    Hope this helps

    Have fun,
    Mark.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Brisbane, Australia
    Posts
    50
    Rep Power
    12
    Originally posted by Wizard2003
    where do you have a string array?
    I don't have string array in my code

    file = open(file, 'r').readlines()
    file would be a string array, isn't it?

    f=open(file, 'r').read(pos)
    this one, f would be just string, is it no?
  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I dont know what this string array is . In C/C++ strings are char arrays but a Python string and list/array are two totally different types..

    readlines() returns a list/array and read() returns a string.

    likewise..

    writelines() writes takes an array and writes it to a file, and write() takes a string.

    If somone could explain the whole string array idea i would really appreciate it.

    Thanks,
    Mark.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo