30
loading...
This website collects cookies to deliver better user experience
getcwd()
function using which we can find out the absolute path of the working directory. # Python program get current working directory using os.getcwd()
# importing os module
import os
# Get the current directory path
current_directory = os.getcwd()
# Print the current working directory
print("Current working directory:", current_directory)
Current working directory: C:\Projects\Tryouts
realpath
method of the os.path module. # Python program get current working directory using os.getcwd()
# importing os module
import os
# Get the current directory path
current_directory = os.getcwd()
# Print the current working directory
print("Current working directory:", current_directory)
# Get the script path and the file name
foldername = os.path.basename(current_directory)
scriptpath = os.path.realpath( __file__ )
# Print the script file absolute path
print("Script file path is : " + scriptpath)
Current working directory: C:\Projects\Tryouts
Script path is : C:\Projects\Tryouts\main.py
# Import the os module
import os
# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))
# Change the current working directory
os.chdir('/Projects')
# Print the current working directory
print("New Current working directory: {0}".format(os.getcwd()))
Current working directory: C:\Projects\Tryouts
New Current working directory: C:\Projects
os.getcwd()
, and if you want to change the current directory, use the os.chrdir()
method.