TypeError: Can't convert 'int' object to str implicitly
Chris Angelico
rosuav at gmail.com
Fri Apr 26 13:28:26 EDT 2013
On Sat, Apr 27, 2013 at 3:23 AM, Dave Angel <d at 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.
ChrisA
More information about the Python-list
mailing list