open a file for read / write

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xx r3negade
    New Member
    • Apr 2008
    • 39

    open a file for read / write

    Sorry for the extreme newbie question, but I honestly can't find this anywhere else. When I do
    Code:
    f = open('/var/www/some_file', 'r+')
    it is supposed to open the file for reading and writing, according to python's documentation. However, it appears to open the file for reading and appending instead. Is there any way I can open the file for reading and writing, without having to call the method twice?
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    If you simply want to clear the contents all you need to do is use truncate. Truncate with no arguments truncates the file to the current position of the file pointer, so by doing:
    [code=python]
    fh.seek(0) # resets file pointer to beginning of file
    fh.truncate() # truncates file contents
    [/code]
    That should clear out the contents of your file.

    Comment

    Working...