Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.getcwdb() Method

Python3 os.getcwdb() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

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

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

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

Syntax

The **getcwdb()** method syntax is as follows:


os.getcwdb()

Parameters

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

(

)

)

Closing a File

os

.

close

(

fd

)

The output of the above program is:


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

Python3 OS File/Directory Methods Python3 OS File/Directory Methods