Changes between Initial Version and Version 1 of Ticket #4276, comment 1
- Timestamp:
- Jun 30, 2015, 5:15:08 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #4276, comment 1
initial v1 1 1 I'm revisiting fixing the problems that my attached solution has and I've come up with a much simpler solution (conceptually). Specifically, if a file is opened in mode 'w', it is automatically truncated to empty, but otherwise retains unix permissions and ownership. So, I'd recommend the following model (and this is more or less what I'm using in my existing code): 2 2 3 --- 3 {{{#!python 4 4 password_lines = open('trac.htpasswd').readlines() 5 5 6 6 (changed, password_lines) = process(password_lines) # i.e. change a password or delete a line or add a line 7 }}} 7 8 8 9 if changed: 10 11 {{{#!python 9 12 open('trac.htpasswd', 'w').writelines(password_lines) 10 --- 13 }}} 11 14 12 15 You should of course break things out and trap file IO exceptions, but this allows you to do a quick proof of concept in ipython (if that's your fancy).