Python has a helper function stat.filemode
to go from the st_mode
(integer) as reported by os.stat
into the familiar "stringy" format (I don't know if this representation has a proper name).
>>> stat.filemode(0o100644)
'-rw-r--r--'
Is there any "unfilemode" helper function to go the other way?
>>> unfilemode('-rw-r--r--')
33188
This is what I tried, but it's producing wrong results. That's not treating the first character denoting file type correctly, and not handling sticky bits etc
table = {
ord('r'): '1',
ord('w'): '1',
ord('x'): '1',
ord('-'): '0',
}
def unfilemode(s):
return int(s.translate(table), 2)