9

From Python:

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

When I check through Bash:

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

Why does this return a different result?

toddmetheny
  • 4,405
  • 1
  • 22
  • 39
FlowRaja
  • 677
  • 3
  • 6
  • 15

1 Answers1

10

This is because your /etc/termcap is a symlink. Let me demonstrate this to you:

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'

Conclusion, use os.lstat instead of os.stat

DevLounge
  • 8,313
  • 3
  • 31
  • 44