By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

open a file for read / write

Xx r3negade
P: 39
Sorry for the extreme newbie question, but I honestly can't find this anywhere else. When I do
Expand|Select|Wrap|Line Numbers
  1. f = open('/var/www/some_file', 'r+')
  2.  
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?
Jun 5 '08 #1
Share this Question
Share on Google+
1 Reply


jlm699
100+
P: 314
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:
Expand|Select|Wrap|Line Numbers
  1. fh.seek(0)     # resets file pointer to beginning of file
  2. fh.truncate()  # truncates file contents
  3.  
That should clear out the contents of your file.
Jun 5 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.