Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between open() and Path.open()

Tags:

python

path

Regarding the pathlib module in the standard library, is the Path.open() method is just a "wrapper" for built-in open() function?

like image 277
Mikhail Zakharov Avatar asked Mar 27 '26 13:03

Mikhail Zakharov


1 Answers

If you read the source code of pathlib.Path.open you'll find that it simply does:

return io.open(str(self), mode, buffering, encoding, errors, newline, opener=self._opener)

and according to io's documentation:

io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

This is an alias for the builtin open() function.

So you are correct that pathlib.Path.open is just a wrapper for the built-in open function.

like image 173
blhsing Avatar answered Mar 30 '26 03:03

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!