Flo*_*aja 9 python bash shell command-line stat
来自Python:
>>> import os
>>> s = os.stat( '/etc/termcap')
>>> print( oct(s.st_mode) )
**0o100444**
Run Code Online (Sandbox Code Playgroud)
当我查看Bash时:
$ stat -f "%p %N" /etc/termcap
**120755** /etc/termcap
Run Code Online (Sandbox Code Playgroud)
为什么这会返回不同的结果?
这是因为你的/ etc/termcap是一个符号链接.让我向你证明这一点:
Bash:
$ touch bar
$ ln -s bar foo
$ stat -f "%p %N" foo
120755 foo
$ stat -f "%p %N" bar
100644 bar
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)
结论,使用os.lstat而不是os.stat