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: Add a class for file operations so a syntax such as open("file.img", File.Write | File.Binary | File.Disk) is possible.
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: isaacsjohnson22, serhiy.storchaka, steven.daprano, xtreak
Priority: normal Keywords:

Created on 2022-02-16 04:56 by isaacsjohnson22, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (8)
msg413315 - (view) Author: Isaac Johnson (isaacsjohnson22) Date: 2022-02-16 04:56
I think it would be great for something like this to be with the IO module. It will improve code readability.
msg413316 - (view) Author: Isaac Johnson (isaacsjohnson22) Date: 2022-02-16 04:57
I'm currently working on implementing this. It will probably be a few weeks.
msg413317 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2022-02-16 06:42
I'm sorry, I don't see why you think this will improve code readability. I also expect it will be harder to teach than the current code.

open("file.img", "wb") just needs the user to learn about reading and writing files, and the difference between binary and text files. It works the same way in probably dozens of different languages.

open("file.img", File.Write | File.Binary | File.Disk) needs the beginner to learn the same details, *plus* they have to learn about this mystery File object, classes, dot notation, `|` the bitwise-or operator, and how to import File from the io module.

My guess is that File.Write etc are just enums equivalent to strings 'w' and 'b', but what's File.Disk?

What else does this File object do?
msg413321 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2022-02-16 08:35
This looks similar to a previous proposal

https://discuss.python.org/t/enum-for-open-modes/2445
msg413322 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-16 08:42
You can create a module on PyPI. If it becomes popular we could consider including in the stdlib.
msg413336 - (view) Author: Isaac Johnson (isaacsjohnson22) Date: 2022-02-16 15:37
Well it wouldn't need to be imported. I was working on including it inside builtins like open(). It wouldn't be very convenient if it needed to be imported.
msg413357 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2022-02-16 21:07
True, but you did say it would be with the io module in your original suggestion.
msg413373 - (view) Author: Isaac Johnson (isaacsjohnson22) Date: 2022-02-17 01:12
Well that is how it works with open. It is implemented in the io module and  added to builtins.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90922
2022-02-17 01:12:34isaacsjohnson22setmessages: + msg413373
2022-02-16 21:07:06steven.dapranosetmessages: + msg413357
2022-02-16 15:37:49isaacsjohnson22setmessages: + msg413336
2022-02-16 08:42:33serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg413322

resolution: rejected
stage: resolved
2022-02-16 08:35:37xtreaksetnosy: + xtreak
messages: + msg413321
2022-02-16 06:42:24steven.dapranosetnosy: + steven.daprano
messages: + msg413317
2022-02-16 05:42:13isaacsjohnson22settype: enhancement
2022-02-16 04:57:08isaacsjohnson22setmessages: + msg413316
2022-02-16 04:56:32isaacsjohnson22create