- The method os.getcwd() in Python returns the current working directory of a process.
- Every process running under an operating system has an associated working directory, which is called as the current working directory of the process.
- The current working directory of a process can be changed by calling the Python method os.chdir().
- While referring any files only the file names or paths relative to the current working directory can be used instead of using absolute paths.
# Get the current working directory
print("The current working directory is:")
# Create a file without specifying any path
# so as to create the file in the current working directory
fileName = "Contents.txt"
fileObject = open(fileName, "w+")
# Print the full path of the newly created file now
print("Full path of the created file is:")
# Now create a new directory
# Change the working directory to the newly created directory
# Print the current working directory
print("Current working directory after changing it:")
The current working directory is:
Full path of the created file is:
Current working directory after changing it: