New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: string method to_file
#70792
Comments
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 Any chance it's feasible? Sadly, I have no real C skills so can't implement myself. :( |
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. |
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. |
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 |
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. |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: