3 ms·
I don't have a machine available right now, but I wonder what happens if two adjacent path elements are integers? Does it perform division instead of path/stri
by metaobject 9y ago
I don't have a machine available right now, but I wonder what happens if two adjacent path elements are integers? Does it perform division instead of path/string concatenation?
- lgas 9y agoTypeError: expected str, bytes or os.PathLike object, not int
- maxerickson 9y agoThe Path object won't construct a path from an integer. >>> from pathlib import Path >>> p=Path(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.6/pathlib.py", line 979, in __new__ self = cls._from_parts(args, init=False) File "/usr/lib/python3.6/pathlib.py", line 654, in _from_parts drv, root, parts = self._parse_args(args) File "/usr/lib/python3.6/pathlib.py", line 638, in _parse_args a = os.fspath(a) TypeError: expected str, bytes or os.PathLike object, not int >>> So what happens if the paths are numbers? They are treated like any other characters.
- sametmax 9y agop=Path(str(1)) Python is strongly typed.
- maxerickson 9y agoYes, that's pretty much the question metaobject was asking. Should I have said "numeric characters" or "numeric strings" in my last paragraph instead of numbers?
- latte 9y agoYou can't concatenate a path with an integer: >>> Path.cwd() / 1 TypeError: argument should be a path or str object, not <class 'int'> Also, note that the '/' operator is left-associative, so even if you have two adjacent numbers when joining paths, there will be no division.