【问题标题】:Why do file permissions show different in Python and bash?为什么文件权限在 Python 和 bash 中显示不同?
【发布时间】:2016-07-28 22:13:17
【问题描述】:
来自 Python:
>>> import os
>>> s = os.stat( '/etc/termcap')
>>> print( oct(s.st_mode) )
**0o100444**
当我检查 Bash 时:
$ stat -f "%p %N" /etc/termcap
**120755** /etc/termcap
为什么会返回不同的结果?
【问题讨论】:
标签:
python
bash
shell
command-line
stat
【解决方案1】:
这是因为您的 /etc/termcap 是一个符号链接。
让我向你演示一下:
重击:
$ touch bar
$ ln -s bar foo
$ stat -f "%p %N" foo
120755 foo
$ stat -f "%p %N" bar
100644 bar
Python:
>>> import os
>>> oct(os.stat('foo').st_mode)
'0100644'
>>> oct(os.stat('bar').st_mode)
'0100644'
>>> oct(os.lstat('foo').st_mode)
'0120755'
>>> oct(os.lstat('bar').st_mode)
'0100644'
结论,用os.lstat代替os.stat