#! /usr/bin/env python # -*- coding: utf-8 -*- import os import stat port="/dev/ttyS0" print "User: {}\nGroup: {}\nEfective User: {}\nEfective Group: {}".format(os.getuid(), os.getgid(), os.geteuid(), os.getegid()) perm = os.stat(port) print "Serial port owner {} can{} read, can{} write and can{} execute".format(perm.st_uid, *[ "" if perm.st_mode & mask else "not" for mask in [stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR]]) print "Serial port group {} can{} read, can{} write and can{} execute".format(perm.st_gid, *[ "" if perm.st_mode & mask else "not" for mask in [stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP]]) print "Serial port others can{} read, can{} write and can{} execute".format(*[ "" if perm.st_mode & mask else "not" for mask in [stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH]]) print "Can we read? {}".format("Yes" if os.access(port, os.R_OK) else "No") print "Can we write? {}".format("Yes" if os.access(port, os.W_OK) else "No") print "Can we execute? {}".format("Yes" if os.access(port, os.X_OK) else "No")