Easy Tutorial
❮ Python Function Python Os Chroot ❯

Python3 os.getcwdb() Method

Python3 OS File/Directory Methods


Overview

The os.getcwdb() method is used to return a bytestring representing the current working directory.

A bytestring is a UTF-8 string that can correspond to any possible byte sequence.

This method was changed in Python 3.8: the function uses UTF-8 encoding on Windows instead of ANSI.

Syntax

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

os.getcwdb()

Parameters

Return Value

Returns a Unicode object representing the current working directory.

Example

The following example demonstrates the use of the getcwdb() 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.getcwdb())

# 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.getcwdb())

# Close the file
os.close(fd)

Executing the above program will output:

Current working directory: b'/var/www/html'
Current working directory: b'/private/tmp'

Python3 OS File/Directory Methods

❮ Python Function Python Os Chroot ❯