Why do file permissions show different in Python and bash?
Asked Answered
A

1

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?

Asparagus answered 7/4, 2016 at 20:7 Comment(0)
S
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

Sateia answered 7/4, 2016 at 21:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.