This is the function for open a file and read the file:
def readFileBytes(
filePath: Union[Path, str]
) -> bytes:
if isinstance(filePath, str):
filePath = Path(filePath)
fileObject = open(filePath, 'rb')
rawBytes = fileObject.read()
fileObject.close()
print("Path: ", filePath)
return rawBytes
And the file path that I want to import is:
/Users/zzzz/Desktop/2023/AI/Project/FinalProject/minecraft/villages/common/iron.nbt
And my path file path is :
/Users/zzzz/Desktop/2023/AI/Project/Final Project/minecraft/test_nbt.py
Whether I use:
path = './villages/commom/animals/cat_black.nbt'
nbt = readFileBytes(path)
or
path = '../villages/common/well_bottom.nbt' # I know it is wrong...
nbt = readFileBytes(path)
It always gives me an error:
fileObject = open(filePath, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'villages/common/well_bottom.nbt'
But if I use /Users/zzzz/Desktop/2023/AI/Project/Final Project/minecraft/villages/common/iron.nbt
, (absolute path) it works.
What is the best/easy way to fix it?