[Tutor] Octal confusion, please explain why. Python 3.2

Jordan wolfrage8765 at gmail.com
Sat Jun 2 13:29:04 CEST 2012


Hello, first off I am using Python 3.2 on Linux Mint 12 64-bit.
I am confused as to why I can not successfully compare a variable that
was created as an octal to a variable that is converted to an octal in a
if statement yet print yields that they are the same octal value. I
think it is because they are transported around within python as a
integer value, is this correct and if so why?
Code example 1: This code works because both are explicitly converted to
Octals.

PATH = '/home/wolfrage/Documents'
DIR_PERMISSIONS = 0o40740

for dirname, dirnames, filenames in os.walk(PATH):
    for subdirname in dirnames:
    #list each subdirectory in this directory.
    full_path = os.path.join(dirname, subdirname)
    print(full_path)
    stat_info = os.stat(full_path)
    uid = stat_info.st_uid
    gid = stat_info.st_gid
    #TODO: Group and User ID Check
    #TODO: Directories need the right Masks, and then read, write, and
    # execute permissions for User(Owner), Group and Others
    #TODO: UMask is seperate from permissions, so handle it seperately.
    if oct(stat_info.st_mode) != oct(DIR_PERMISSIONS):
        #TODO: Fix User Read Permissions
        print(str(oct(stat_info.st_mode)) + ' Vs ' +
str(oct(DIR_PERMISSIONS)))
        os.chmod(full_path, DIR_PERMISSIONS)
        print(subdirname + ' has bad user permissions.')


Code Example 2: This code does not work because we do not use an
explicit conversion.

PATH = '/home/wolfrage/Documents'
DIR_PERMISSIONS = 0o40740

for dirname, dirnames, filenames in os.walk(PATH):
    for subdirname in dirnames:
    #list each subdirectory in this directory.
    full_path = os.path.join(dirname, subdirname)
    print(full_path)
    stat_info = os.stat(full_path)
    uid = stat_info.st_uid
    gid = stat_info.st_gid
    #TODO: Group and User ID Check
    #TODO: Directories need the right Masks, and then read, write, and
    # execute permissions for User(Owner), Group and Others
    #TODO: UMask is seperate from permissions, so handle it seperately.
    if oct(stat_info.st_mode) != DIR_PERMISSIONS:
        #TODO: Fix User Read Permissions
        print(str(oct(stat_info.st_mode)) + ' Vs ' + str(DIR_PERMISSIONS))
        # The Above print statement shows that DIR_PERMISSIONS is
printed as an
        # Integer. But Why does Python convert a explicitly created
Octal to an
        # Integer?
        os.chmod(full_path, DIR_PERMISSIONS)
        print(subdirname + ' has bad user permissions.')




More information about the Tutor mailing list