4 ms·
why replace open() with the Path().read_text()? the open() API transcends Python and is almost ubiquitous. that suggestion seems frivolous. while we are talkin
by blondin 4y ago
why replace open() with the Path().read_text()? the open() API transcends Python and is almost ubiquitous. that suggestion seems frivolous.
while we are talking modern Python, the type hints fever seems to be receding. such a good thing in my opinion. i expected this tool to bark about types but am glad the author didn't go that far.
nice tool.
- dosisod 4y agoSomething that I forgot to mention in the other answers similar to this is that the Path().read_text() check only applies if the open() block is a single line, and that line is only used to read the contents of the file. If you are doing other things in that block, no error will be emitted. I agree that open() is pretty ubiquitous, but in certain cases (ie, if I am already using a Path object), using read_text() is a nice one-liner.
- bityard 4y agoread_text() is simpler and _much_ clearer (and thus, Pythonic) when you're doing trivial file operations like reading the contents into a variable. Opening up a whole context manager is still an option when you have to do more complicated stuff to the file.