Easy Tutorial
❮ Python Mysql Python Selection Sort ❯

Python3 os.getcwd() Method

Python3 OS File/Directory Methods


Overview

The os.getcwd() method is used to return the current working directory.

Syntax

The syntax for the getcwd() method is as follows:

os.getcwd()

Parameters

Return Value

Returns the current working directory of the process.

Example

The following example demonstrates the use of the getcwd() method:

Example

#!/usr/bin/python3

import os, sys

# Change to the "/var/www/html" directory
os.chdir("/var/www/html")

# Print the current directory
print("Current working directory: %s" % os.getcwd())

# Open "/tmp"
fd = os.open("/tmp", os.O_RDONLY)

# Change directory using os.fchdir() method
os.fchdir(fd)

# Print the current directory
print("Current working directory: %s" % os.getcwd())

# Close the file
os.close(fd)

Executing the above program outputs the following:

Current working directory: /var/www/html
Current working directory: /tmp

Python3 OS File/Directory Methods

❮ Python Mysql Python Selection Sort ❯