The value of os.getcwd() in python appears in the parent folder.

Asked 6 days ago, Updated 6 days ago, 2 views

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

2022-09-20 15:05

1 Answers

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



2022-09-20 15:05

If you have any answers or tips


© 2022 pinfo. All rights reserved.