Python3 os.getcwdb() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.getcwdb() method returns a bytestring representing the current working directory.

ByteString is a UTF-8 string that can correspond to all possible byte sequences.

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

Syntax

getcwdb()The method syntax is as follows:

os.getcwdb()

Parameters

  • None

Return Value

Returns a Unicode object of the current working directory.

Example

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

Example

#!/usr/bin/python3

import os, sys

# Switch 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 )

# Use the os.fchdir() method to change the directory
os.fchdir(fd)

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

# Close the file
os.close( fd )

Executing the above program outputs the following result:

当前工作目录 : b'/var/www/html'
当前工作目录 : b'/private/tmp'

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions