[Python-ideas] Rewriting file - pythonic way

2018-04-15 Thread Alexey Shrub
Hi all, I am new in python (i am moving from Perl world), but I always love Python for hight level, beatuful and clean syntax. Now I have question/idea about working with files. On mine opinion it very popular use case: 1. Open file (for read and write) 2. Read data from file 3. Modify data.

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-15 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 12:40 , Serhiy Storchaka написал: If the problem is that you want to use a single line instead of three line, you can add a function Yes, I think that single line with word 'rewrite' is much more readable than those three lines. And yes, I

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 1:12 , Serhiy Storchaka написал: Actually the reliable code should write into a separate file and replace the original file by the new file only if writing is successful. Or backup the old file and restore it if writing is failed. Or do both.

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 10:47 , George Fischhof написал: https://docs.python.org/3/library/fileinput.html https://pypi.python.org/pypi/in-place looks not bad too ___ Python-ideas mailing list Python-ideas@python.org

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 6:19 , Oleg Broytman написал: Can I recommend to catch exceptions in `backuper.backup()`, cleanup backuper and unlock locker? Yes, thanks, I move .backup() to try, about other exception I think that it must be catched outside, because this

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
https://pypi.python.org/pypi/in-place > * Instead of hijacking sys.stdout, a new filehandle is returned for writing. > * The filehandle supports all of the standard I/O methods, not just readline(). why fileinput did not support this things? ___

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 10:47 , George Fischhof написал: https://docs.python.org/3/library/fileinput.html Thanks, it works https://github.com/worldmind/scripts/blob/master/filerewrite/fileinputtest.py but looks like that way only for line by line processing

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-15 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 2:40 , Nick Coghlan написал: https://bugs.python.org/issue8604#msg174104 is the relevant tracker discussion Thanks all, I agree that universal and absolutly safe solution is very difficult, but for experiment I made some draft

Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Понедельник, 16 апр. 2018 в 2:48 , Alexey Shrub <ash...@yandex.ru> написал: https://pypi.python.org/pypi/in-place I like in_place module https://github.com/worldmind/scripts/blob/master/filerewrite/inplacetest.py it fix some strange features of fileinput module. Maybe in_plac