This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Feature request: string method `to_file`
Type: enhancement Stage: resolved
Components: IO Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, nickeubank, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2016-03-21 17:00 by nickeubank, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg262141 - (view) Author: nickeubank (nickeubank) Date: 2016-03-21 17:00
As a social scientists trying to help other social scientists move from language like R, Stata, and Matlab into Python, one of the behaviors I've found unnecessarily difficult to explain is the "file.open()/file.close()" idiom (or, alternatively, context managers). In normal operating systems, and many high level languages, saving is a one-step operation. 

I understand there are situations where an open file handle is useful, but it seems a simple `to_file` method on strings (essentially wrapping a context-manager) would be really nice, as it would save users from learning this idiom. 

Any chance it's feasible? Sadly, I have no real C skills so can't implement myself. :(
msg262142 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-21 17:03
It's easy to implement such function in a module and upload such module to PyPI (ex: "to_file" module :-)).

It's not easy to guess user expectations on such function: text encoding, buffering, etc.

Adding a method to the builtin str type which writes into a file is a no-no.
msg262143 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-03-21 17:14
Such function already exists in the stdlib! But with different order of arguments. See pathlib.Path.write_text().

Personally I'm very skeptical about usefulness of similar functions in the stdlib.
msg262144 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-21 17:16
Oh. I wasn't aware of these pathlib methods.

https://docs.python.org/dev/library/pathlib.html#pathlib.Path.write_bytes

https://docs.python.org/dev/library/pathlib.html#pathlib.Path.write_text
msg262155 - (view) Author: nickeubank (nickeubank) Date: 2016-03-22 00:18
Seems like existence of a method in the stdlib (or at least in the pathlib part of the stdlib) suggests issues with text encoding can be overcome. 

IMHO I would suggest most people have no idea about that method (I've been doing python for years and this has always been a personal frustration, and I've asked several others for better options and no one had one to offer),  and it seems like it would make much more sense as a string method. If someone has a string they want to save to disk, I can't imagine anyone looking in the Path library. 

I respect the desire to avoid bloat -- the context manager or open/close idiom has just felt unnecessarily complicated (dare I say unpythonic?) for a common task. 

Also, I think it's one that data scientists use a lot when they generate outputs to pull into LaTeX documents.
msg262169 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2016-03-22 07:58
Those methods were added in 3.5, so no wonder many people would be unaware of them. I would suggest first taking such a proposal to python-ideas, since bug tracker is not a venue for proposing/advocating such sweeping changes to the language.
History
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70792
2016-03-22 07:58:11SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg262169

resolution: not a bug
stage: resolved
2016-03-22 00:18:53nickeubanksetmessages: + msg262155
2016-03-21 17:16:28vstinnersetmessages: + msg262144
2016-03-21 17:14:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg262143
2016-03-21 17:03:20vstinnersetnosy: + vstinner
messages: + msg262142
2016-03-21 17:00:15nickeubankcreate