Search code examples
pythonfilefilesystemspython-os

Windows vs Linux file modes


On a windows machine, I am trying to get a file's mode using the os module in python, like this (short snippet):

import os
from stat import *

file_stat = os.stat(path)
mode = file_stat[ST_MODE]

An example for the mode I got for a file is 33206.

My question is, how can I convert it to the linux-file mode method? (for example, 666).

Thanks to all repliers!

Edit:

found my answer down here :) for all who want to understand this topic further:

understanding and decoding the file mode value from stat function output


Solution

  • Check if this translates properly:

    import os
    import stat
    
    file_stat = os.stat(path)
    mode = file_stat[ST_MODE]
    print oct(stat.S_IMODE(mode))
    

    For your example:

    >>>print oct(stat.S_IMODE(33206))
    0666
    

    Took it from here. Read for more explanation