[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
New submission from Olemis Lang olemis...@gmail.com: Often I have the contents to be written in a file at a given path that I know as well. I recently tried to find a function in stdlib to do that and to my surprise this is what I found : - Such function exists - It's `distutils.file_util.write_file` IMO the last place where people'd look for such a function is inside `distutils` package. Besides I reviewed modules listed under `File and directory access` category in `Library Reference` and found nothing even similar. The idea is to provide a a similar function in `shutils` module -- assignee: tarek components: Distutils messages: 104833 nosy: olemis, tarek priority: normal severity: normal status: open title: Alias for distutils.file_util.write_file in e.g. shutils type: feature request versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Antoine Pitrou pit...@free.fr added the comment: This sounds silly to me. You can write a file in two lines: with open(foo, wb) as f: f.write(contents) If you want to do something more useful, you can add a function for atomic writing of a file. -- nosy: +pitrou versions: +Python 3.2 -Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Eric Smith e...@trueblade.com added the comment: Does the standard library really need something so trivial? I'd put it in your own program. And I'd make the one in distutils private (and fix it to use a with statement). -- nosy: +eric.smith ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Tarek Ziadé ziade.ta...@gmail.com added the comment: @Eric: remember that distutils is frozen, so it won't be removed. And historically Distutils code was meant to be 2.4 compatible (thus, no with) @Antoine: Yes, that would be the idea (provide a robust pattern by using a temporary file, then rename it) -- components: +Library (Lib) -Distutils ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Antoine Pitrou pit...@free.fr added the comment: @Antoine: Yes, that would be the idea (provide a robust pattern by using a temporary file, then rename it) Then perhaps it would be better as a context manager: with shutil.atomic_write(foo, wb) as f: f.write(mycontents) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Changes by Dan Buch daniel.b...@gmail.com: -- nosy: +meatballhat ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Tarek Ziadé ziade.ta...@gmail.com added the comment: Well, a context manager sounds overkill since we just want to write some content in a file (and nothing else during that context). I think a simple call is straightforward: shutil.atomic_write(foo, contents, mode=wb) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Tarek Ziadé ziade.ta...@gmail.com added the comment: Notice that contents could be replaced here by an iterator, that would return data to send to write() -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Antoine Pitrou pit...@free.fr added the comment: Well, a context manager sounds overkill since we just want to write some content in a file (and nothing else during that context). Using a context manager, though, is the recommended idiom to write files. I think there's a value in remaining consistent. We don't want to end up like PHP which has dozens of different idioms of doing similar things. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Tarek Ziadé ziade.ta...@gmail.com added the comment: This idiom makes sense when you want to do some things with an open file, and replaces the usual try..finally idiom. That's not what we want to do here. We want to write data in a file in a single step, in an atomic manner. Giving the ability to the developers to work in a context manager means that you potentially give them the ability to break this atomicity. So I don't think the context manager idiom prevails, and should be avoided : shutil.atomic_write is asked to write a file, given some data, and don't return until it's done. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Jean-Paul Calderone exar...@twistedmatrix.com added the comment: Considering that it's extremely difficult to implement this correctly and in a cross-platform way, I think it makes sense as a stdlib addition (though I'd add it as a method of a `path` type rather than to the shell utilities module ;). -- nosy: +exarkun ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Eric Smith e...@trueblade.com added the comment: I agree that with the addition of the new requirement that it be an atomic write, it should be in a library. I'd also do it as a context manager, since that's the more general case. distutils2 can either call the context manager version, or have a trivial function that calls the context manager. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Antoine Pitrou pit...@free.fr added the comment: Giving the ability to the developers to work in a context manager means that you potentially give them the ability to break this atomicity. AFAICT this doesn't make sense. The writing isn't atomic, the renaming is. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8604] Alias for distutils.file_util.write_file in e.g. shutils
Tarek Ziadé ziade.ta...@gmail.com added the comment: @Antoine, @Eric: After some more thoughts a contextlib makes more sense. I also have a use case for an atomic copytree() but I guess that can be an option in copytree() -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8604 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com