Python 3 reference · Built-in Functions

Python oct() Function

Convert an integer to an octal string prefixed with 0o, such as oct(8) giving '0o10'. Often used for file permissions.

Python 3 Since 3.0

Syntax

oct(x)

Parameters

ParameterDescription
x An integer.

Return value

A string such as '0o755'.

Examples

To octal and backTry it yourself
print(oct(8), oct(493), oct(-56))
print(int("755", 8))

Output

0o10 0o755 -0o70
493
Unix permissionsTry it yourself
mode = 0o644  # rw-r--r--
print(oct(mode), f"{mode:o}")

Output

0o644 644

Version history

VersionChange
Python 3.0Added