Windows vs Linux file modes

985 views Asked by At

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

2

There are 2 answers

1
Mohammad Yusuf On BEST ANSWER

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

1
dejanualex On

One workaround would be to use:os.system(r'attrib –h –s d:\your_file.txt') where you can use the attribute switches : R – This command will assign the “Read-Only” attribute to your selected files or folders. H – This command will assign the “Hidden” attribute to your selected files or folders. A – This command will prepare your selected files or folders for “Archiving.” S – This command will change your selected files or folders by assigning the “System” attribute.