<div dir="ltr">Hi, <div><br></div><div>some similar thing already exist in standard:</div><div><a href="https://docs.python.org/3/library/fileinput.html">https://docs.python.org/3/library/fileinput.html</a><br></div><div><br></div><div>fileinput(... inplace=True...)</div><div><br></div><div>BR,</div><div>George</div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-04-15 10:57 GMT+02:00 Alexey Shrub <span dir="ltr"><<a href="mailto:ashrub@yandex.ru" target="_blank">ashrub@yandex.ru</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
I am new in python (i am moving from Perl world), but I always love Python for hight level, beatuful and clean syntax.<br>
Now I have question/idea about working with files.<br>
On mine opinion it very popular use case:<br>
1. Open file (for read and write)<br>
2. Read data from file<br>
3. Modify data.<br>
4. Rewrite file by modified data.<br>
<br>
But now it is looks not so pythonic:<br>
<br>
with open(filename, 'r+') as file:<br>
data = file.read()<br>
data = data.replace('old', 'new')<br>
file.seek(0)<br>
file.write(data)<br>
file.truncate()<br>
<br>
or something like this<br>
<br>
with open(filename) as file:<br>
data = file.read()<br>
data = data.replace('old', 'new')<br>
with open(filename) as file:<br>
file.write(data)<br>
<br>
I think best way is something like this<br>
<br>
with open(filename, 'r+') as file:<br>
data = file.read()<br>
data = data.replace('old', 'new')<br>
file.rewrite(data)<br>
<br>
but for this io.BufferedIOBase must contain rewrite method<br>
<br>
what you think about this?<br>
<br>
<br>
<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
</blockquote></div><br></div>