Absolute path with .py file is
c:/pythonworkspace/test/test.py.
The return value of os.getcwd() is c:/pythonworkspace.
Development environment visualstudiocode 1.6.0 python 3.9.x It's windows 10.
python
The location of the py file and the execution path (current path) have nothing to do with it.
Look at the following example.
## test.py
import os
from pathlib import Path
print("getcwd=" + os.getcwd())
path = Path(__file__)
print("path.parent=" + str(path.parent.absolute()))
C:\pythonworkspace\test>py test.py
getcwd=C:\pythonworkspace\test
path.parent=C:\pythonworkspace\test
C:\pythonworkspace\test>cd ..
C:\pythonworkspace>py test\test.py
getcwd=C:\pythonworkspace
path.parent=C:\pythonworkspace\test
C:\pythonworkspace>cd ..
C:\>py pythonworkspace\test\test.py
getcwd=C:\
path.parent=C:\pythonworkspace\test
© 2024 OneMinuteCode. All rights reserved.