On 04/26/2013 01:28 PM, Chris Angelico wrote:
On Sat, Apr 27, 2013 at 3:23 AM, Dave Angel <d...@davea.name> wrote:
On 04/26/2013 11:05 AM, Chris Angelico wrote:
I've checked out what fileinput.input() is doing here (ought to have
done that earlier, sorry!) and I now understand this block of code
more. You're replacing that word _in the file, on disk_, and then
making the inverse replacement. This strikes me as dangerous; if
anything happens to your process in the middle, the file will be
damaged on disk. I would STRONGLY recommend rewriting this to use some
other file - for instance, a temporary file.
fileinput.Fileinput class already creates the temp file when you specify
inplace=True
If it didn't, I'd also have to point out the hazards of doing in-place
updates in a text file where the new data and old is a different length.
There still may be reasons to make an explicit backup, but I don't know what
they are.
That's true if something goes wrong during the actual writing of the
file only. But if the process bombs in the middle of the execution
phase (which in the Python script is a single os.system() call), then
the file will have been one-way changed on the disk - hence,
"damaged". The explicit temporary file (and executing the temp file)
is a much safer way to do it.
Only two of those sentences makes sense to me. I have no idea what
"execution phase" means, and don't know what would be done via an
os.system() call. I don't know what "one-way change" means
If I were specifying the fileinput stuff, I'd have said the new data
should be written to the temp file, so that at no time was the original
file in an in-between state.
--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list