📅  最后修改于: 2023-12-03 14:38:59.673000             🧑  作者: Mango
When working with paths in Python, it is important to keep in mind the type of data being passed as the path. This error message is encountered when the path being passed is not of the correct type, specifically not a string, bytes, or os.PathLike object. Instead, a list was passed, which is not a valid type of path.
To prevent this error message, ensure that the path being passed is a valid type, such as a string or bytes object. It is also recommended to use os.PathLike objects, which are designed to be platform-independent and work across different operating systems.
Here is an example code snippet that may encounter this error:
import os
path = ['folder1', 'folder2', 'filename.txt']
full_path = os.path.join(path)
os.path.abspath(full_path)
This code attempts to construct a path using a list of folder names and a file name. However, the os.path.join()
function expects its arguments to be strings, bytes, or os.PathLike objects. Since a list is passed, the function raises the _getfullpathname
error.
To fix this error, the list should be converted to a string before being passed to os.path.join()
. Here is an updated code snippet that should not encounter the error:
import os
path = ['folder1', 'folder2', 'filename.txt']
full_path = os.path.join(*path)
os.path.abspath(full_path)
In this code, the asterisk operator is used to unpack the list into separate arguments to os.path.join()
. This results in the expected output, and no error message is raised.
Overall, it is important to always ensure that the correct type of data is being passed as a path in Python, in order to avoid errors such as _getfullpathname
.