FlowRaja
FlowRaja

Reputation: 727

Why do file permissions show different in Python and bash?

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?

Upvotes: 9

Views: 252

Answers (1)

DevLounge
DevLounge

Reputation: 8437

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

Upvotes: 10

Related Questions