Scripting >> Python >> Examples >> Function to convert Unix octal permissions to string (rwx) permissions

def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for odigit in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if odigit >= value:
                result += letter
                odigit -= value
                result += "-"
    return result
print(octal_to_string(755)) # should return rwxr-xr-x