import os
print(os.getcwd())
import os
#Get Current working Directory
currentDirectory = os.getcwd()
#Change the Current working Directory
os.chdir('/home/varun')
import os
os.getcwd()
# print current working directory in python
import os
cwd = os.getcwd()
print(cwd)
import os
current_working_directory = os.getcwd()
print(current_working_directory) # should print the cwd
""" Bonus: If you want to change cwd, without moving file,
use the following method"""
os.chdir("path/to/directory")
# If by "current working directory" you mean
# the directory in which is saved the script in execution, then:
import os
cwd = os.path.dirname(os.path.realpath(__file__))
print(cwd)
>>> import os
>>> os.getcwd()
'C:Program FilesPyScripter'
>>> os.getcwdb()
b'C:Program FilesPyScripter'
import os
os.getcwd()
import pathlib
print(pathlib.Path(__file__).parent.absolute())
import os
print("File location using os.getcwd():", os.getcwd())
print(f"File location using __file__ variable: {os.path.realpath(os.path.dirname(__file__))}")