TypeError: Can't convert 'int' object to str implicitly

Chris Angelico rosuav at gmail.com
Fri Apr 26 14:02:20 EDT 2013


On Sat, Apr 27, 2013 at 3:52 AM, Dave Angel <davea at davea.name> wrote:
> On 04/26/2013 01:28 PM, Chris Angelico wrote:
>>
>> 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.
>>
>
> 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.

Here's a massive simplification of the OP's program:

1. Build a list of device IDs
2. Edit the file "firstdev.ahk" and replace all instances of "device"
with the device ID
3. Execute the now-edited firstdev.ahk using os.system()
4. Reverse the edit of firstdev.ahk, replacing all instances of the
device ID with the word "device".

Apart from the risk of accidentally changing back something that
wasn't changed in the first place (which the OP may know to be
impossible, eg if the macro file has no numbers in it), this has the
risk that a computer failure in step 3 will leave the file on disk in
its edited state. That's what I'm concerned about. By writing the
modified .ahk content to a different file and then executing the other
file, he would avoid editing the template at all. It'd also then be
multi-process safe, for what that's worth.

ChrisA



More information about the Python-list mailing list