1

os module contains function chflags that allows user to modify UNIX file flags: https://docs.python.org/3/library/os.html

Is there a function that would let me see which flags the file currently has?

I am asking about file flags (equivalent of unix command lsattr or chattr) not permissions (chmod etc)

Petr
  • 13,747
  • 20
  • 89
  • 144

1 Answers1

3

File flags are made available via os.stat().

The POSIX implementation of python's os module returns a stat_result object that has an st_flags field:

import os
st = os.stat(filename)
print(st.st_flags)

The possible flag bits are available in the stat module as

SF_*
UF_*

constants.

dhke
  • 15,008
  • 2
  • 39
  • 56
  • 1
    I tried this and get `AttributeError: 'os.stat_result' object has no attribute 'st_flags'` – Petr Mar 21 '15 at 11:29
  • 1
    According to the documentation this attribute may not be available on some Unix systems. Could you please describe your setup? – Horst Gutmann Mar 21 '15 at 11:36
  • It's ubuntu 14.04 LTS, but now I figured out that my os module doesn't even have chflags function :( maybe it's not in my python? I got python 3.4.0 – Petr Mar 21 '15 at 11:39
  • Linux does not have a `chflags()` system call. It is simply not supported by the operating system. – dhke Mar 21 '15 at 11:39
  • 1
    However Linux has a [`chattr`](http://en.wikipedia.org/wiki/Chattr) function and this should/could be available through `os.chflags()`. – davidedb Mar 21 '15 at 21:01
  • @davidedb Unfortunately, no. `chattr` is not made available via the `os` module even on Linux. Sounds like a good candidate for a feature request, though. – dhke Mar 23 '15 at 07:11
  • @dhke In fact the point is that the Linux `chattr` functionalities should be (partially?) available through `os.chflags()`. See also [Python os module lacks the chflags/lchflags methods](https://bugs.launchpad.net/ubuntu/+source/python2.7/+bug/969032). – davidedb Mar 23 '15 at 09:00