Slowly.For: basestringIn Python use 3 as appropriate str or os.PathLike orbytes, str) or if it'sinstance to try https://ru.stackoverflow.com/q/414593/23044 ♪Argument is textIf you work with the Piton text, use Unicode. In Python 2, the text in the intersection could have been presented both bytes and by Unicode (no wonder that the interface is similar). The sheets representing a coded text should be decoded in Unicode before further use in the programme, otherwise easy to obtain https://ru.stackoverflow.com/q/229015/23044 ♪In Piton 3, just use it. isinstance(text, str) where in this case Pitone 2 would be required isinstance(text, basestring)♪Argument is the name of the file.Some data can almost always be the text, but may not be formally worded. For example, often the names of files can be coded using sys.getfilesystemencoding() Coding, but it's not always possible for a file to be known as "nix." '/' and zero '\0'. Details https://ru.stackoverflow.com/q/527815/23044 In this case, both the Unicode and the baytes could be taken by the tracks and therefore used basestring in Pitone 2, where isinstance is justified. Pitton 3 can be used https://docs.python.org/3.6/library/os.html#os.PathLike if available, which allows not only bytes, str, but also pathlib.Path♪ os.DirEntry(sighs)os.scandir()) Eid to present the names of the files. More likely that https://docs.python.org/3.6/library/os.html#os.fspath EAFP, instead isinstance(path, os.PathLike) (LBYL style). https://github.com/python/cpython/blob/6463ec562047fcf5133b5ace2b524118621020c5/Lib/posixpath.py#L367-L376 :def abspath(path):
"""Return an absolute path."""
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
cwd = os.getcwdb()
else:
cwd = os.getcwd()
path = join(cwd, path)
return normpath(path)
abspath() accepts any os.PathLike object. It uses OS API, which only str♪ bytes I know.os.fspath() guaranteed only str♪ bytes returns.(str, bytes)In practice, it is sometimes necessary to adopt both baytes and text. For example, subprocess.Popen accepts command lines as bytes and str (what OS API provides). There is no ABC in Pitone that would represent the lines derived from the system (command frames, variable environments, paths), so instead os.SystemString♪ Popen() Use it. (str, bytes) When it's necessary to check. The modern interface would only take the lines here (str) and call out. os.fsencode() In cases where OS requires bytes. sys.argv♪ os.environ It's a str collection in Piton.surrogateescape may be used. Relatively recent addition to standard library pathlib.Path accepts only the lines (str).From the comment below:♪ ♪ The use of isinstance for the classification of values is appropriate when the function logic depends on the type of argument adopted. An example is self-instance, which (in a statically typified CP)) should only take the listed type in the second argument, but (in Python) allows for a separate type to be transferred instead of a cortege/list from one element.Popen() This is an example: it accepts the team.args) as a line or as a list/piece of lines. Direct quote https://github.com/python/cpython/blob/6463ec562047fcf5133b5ace2b524118621020c5/Lib/subprocess.py#L1203-L1206 : if isinstance(args, (str, bytes)):
args = [args]
else:
args = list(args)
Another example, fileinput.input() a function that accepts either the file or the list/box section. From https://github.com/python/cpython/blob/6463ec562047fcf5133b5ace2b524118621020c5/Lib/fileinput.py#L190-L198 : if isinstance(files, str):
files = (files,)
else:
if files is None:
files = sys.argv[1:]
if not files:
files = ('-',)
else:
files = tuple(files)
In this case, fileinput Not updated for use os.PathLikeI'm just using it. str (where Python 2.7 would be used) basestring(d) Code climatics are forced on their own os.fspath() Call when necessary.