Not really, as you can see in the pathlib module exist 2 types of path classes:
- pure path classes {PurePath, PurePosixPath, PureWindowsPath}
- concrete path classes {Path, PosixPath, WindowsPath}.
Parameters of theses classes constructors will be just *pathsegments.
And if you look at the available read/write methods (read_text/read_bytes and write_text/write_bytes) you'll also see mode won't be available neither
So, as you've already discovered, the only way you can use mode with these pathlib classes is by using open method, ie:
with my_path.open("a") as f:
f.write("...")
This is by design and that way the pathlib classes have become really "clean". Also, the above snippet is already canonical so it can't be simplified any further. You could use open method outside the context manager though:
f = my_path.open("a")
f.write("...")
Answer from BPL on Stack OverflowNot really, as you can see in the pathlib module exist 2 types of path classes:
- pure path classes {PurePath, PurePosixPath, PureWindowsPath}
- concrete path classes {Path, PosixPath, WindowsPath}.
Parameters of theses classes constructors will be just *pathsegments.
And if you look at the available read/write methods (read_text/read_bytes and write_text/write_bytes) you'll also see mode won't be available neither
So, as you've already discovered, the only way you can use mode with these pathlib classes is by using open method, ie:
with my_path.open("a") as f:
f.write("...")
This is by design and that way the pathlib classes have become really "clean". Also, the above snippet is already canonical so it can't be simplified any further. You could use open method outside the context manager though:
f = my_path.open("a")
f.write("...")
There is no "append mode" for write_text, nor is there a corresponding append_text method. If you need it, you can write a function for it yourself pretty easily:
def append_text(path, text, encoding=None, errors=None):
with path.open("a", encoding=encoding, errors=errors) as f:
f.write(text)
You might be wondering why such a thing isn't built directly into pathlib as a method. One reason is precisely because it is so easy to implement yourself that it's not really worth adding, but clearly that's not the whole story, because read_text and write_text would be similarly easy to implement yourself.
I think a major reason why pathlib.Path objects don't (and, indeed, shouldn't) have a append_text method is because it creates a hole for inexperienced users to fall into, which is a huge sin in API design.
Specifically, the hole I'm referring to is using append_text on the same file repeatedly in a loop. Because you're continually opening and closing the file, it is slow. Plus, doing so many unnecessary writes is probably not great for the health of your hard drive.
Worse, because the program will actually behave correctly (e.g. the file will have the contents they intended), they may not even notice anything is wrong, because they don't necessarily have a mental gauge on how long writing to a file "should" take.
This is exactly the sort of code a naïve programmer would write:
from pathlib import Path
N = 100
path = Path("numbers.txt")
path.write_text(f"{N}\n")
for i in range(N):
path.append_text(f"{i}\n")
Hi, in the online Python course im following, the instructor used a “with … as … “ statement for opening a file so he could iterate with a for loop later. Without much further explanation.
I like to learn by building a strong foundation, so if i see something i don’t know yet, or want to know more about, i look it up in the book ‘python crash course 3rd edition’.
To my surprise and great confusion, there is nothing about ‘with’ statements in the book. After some research i found that that method was used and explained in the 2nd edition, but in the 3rd edition it’s completely replaced by using the ‘pathlib’ method. This method seems easy and straightforward, but since it takes the whole content of a file as one single string, and i want to iterate over separate lines with a for loop, i need to cut it up first using ‘.splitlines’. That’s explained pretty well in the 3rd edition, so no problem there.
But i do have some questions now: Why the change to pathlib? Should i just use this method from now on, and forget about the ‘with’ statement stuff? What method of the two do you guys prefer?