python - 为什么文件权限在 Python 和 bash 中显示不同?

标签 python bash shell command-line stat

来自 Python:

>>> import os
>>> s = os.stat( '/etc/termcap')
>>> print( oct(s.st_mode) )
**0o100444**

当我检查 Bash 时:

$ stat -f "%p %N" /etc/termcap
**120755** /etc/termcap

为什么这会返回不同的结果?

最佳答案

这是因为您的/etc/termcap 是一个符号链接(symbolic link)。 让我向您演示一下:

bash :

$ 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

关于python - 为什么文件权限在 Python 和 bash 中显示不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36486194/

相关文章:

java - 超越 Python 中的工厂

jquery - 在单击 prev 按钮时使用 django View 更新 jquery fullcalendar 中的事件

linux - 如何使用 bash 在 Linux 上删除多个文件中的多行

xml - 查找字符串并替换为换行符和缩进

linux - 在 shell 脚本中使用函数时如何记录压缩的 zip 文件

python - Pandas:将数据 append 到 pandas 数据框中的问题

javascript - 如何编写自己的脚手架(像 RoR 那样?)

shell - 从输出中 grep 特定变量并将值替换为另一个 linux

android - 使用 adb shell 禁用优先级为 ERROR 的垃圾邮件日志

python - 为什么Tornado的ioloop和httpserver的性能有差异?