os.getcwdb() method returns the current working directory as a bytes object. It is similar to os.getcwd(), but instead of returning a string, it returns the directory path in bytes format.
Example: In the following example, we get the current working directory as a bytes object.
import os
print(os.getcwdb())
Output
b'/home/user'
Explanation: os.getcwdb() returns the current working directory in bytes format. The prefix b indicates that the result is a bytes object.
Syntax
os.getcwdb()
Return Value: Returns the current working directory as a bytes object.
Examples
Example 1: The following example stores the current working directory in a variable and prints it. This can be useful when the directory path is needed later in the program.
import os
cwd = os.getcwdb()
print(cwd)
Output
b'/home/user/projects'
Explanation: os.getcwdb() returns the current directory as bytes and stores it in cwd.
Example 2: The following example verifies the data type returned by os.getcwdb().
import os
cwd = os.getcwdb()
print(type(cwd))
Output
<class 'bytes'>
Explanation: type(cwd) shows that os.getcwdb() returns a bytes object, not a string.
Example 3: The following example converts the bytes path returned by os.getcwdb() into a regular string.
import os
cwd = os.getcwdb()
print(cwd.decode())
Output
/home/user/projects
Explanation: cwd.decode() converts the bytes object returned by os.getcwdb() into a string representation of the directory path.