Windows vs Linux文件模式

时间:2016-12-22 09:32:59

标签: python file filesystems python-os

在Windows机器上,我试图在python中使用os模块获取文件模式,就像这样(短片段):

import os
from stat import *

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

我为文件获取的模式示例是33206.

我的问题是,如何将其转换为linux-file模式方法? (例如,666)。

感谢所有回复者!

修改

在这里找到我的答案:)给所有想进一步理解这个主题的人:

understanding and decoding the file mode value from stat function output

2 个答案:

答案 0 :(得分:3)

检查这是否正确翻译:

import os
import stat

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

对于你的例子:

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

Took it from here. Read for more explanation

答案 1 :(得分:0)

一种解决方法是使用:os.system(r'attrib –h –s d:\your_file.txt'),您可以使用属性开关: R - 此命令将“只读”属性分配给所选文件或文件夹。 H - 此命令将“隐藏”属性分配给所选文件或文件夹。 A - 此命令将为“存档”准备所选文件或文件夹。 S - 此命令将通过分配“系统”属性来更改所选文件或文件夹。