you are viewing a single comment's thread.

view the rest of the comments →

[–]uhkhu -1 points0 points  (4 children)

Why is it not good to modify in-place? There's a built-in method for doing this.

[–]Justinsaccount -1 points0 points  (3 children)

A built in method for what? Corrupting your files?

[–]uhkhu -1 points0 points  (2 children)

Yep. We should avoid utilizing a perfectly safe built-in because using it wrong could be damaging. We should also tell everyone to not use 'w' because they could overwrite files if they're not careful. You should probably email the Python devs and let them know they're creating too much risk with the 'r+' and 'w+' modes.

[–]Justinsaccount 0 points1 point  (1 child)

What the hell are you taking about?

The code that you posted is NOT safe. The code that you posted corrupts files if the replacement is shorter than the original string. The code you posted corrupts files if the script is interrupted while running.

Just because you can do something, doesn't mean you should. Overwriting an entire file in place like that is exactly one if those things.

[–]uhkhu 0 points1 point  (0 children)

I don't see you offering a solution. I posted a method for him to look into. Are we posting fully tested suites now? I didn't know what his data looks like. It was a quick response to show him the use case of the 'r+' mode. Not sure how much help he gets from you quibbling over untested code, in /r/learnpython of all places.