我想创建一个脚本,该脚本将文件名作为参数并以符号形式打印其权限。
假设八进制权限是755,脚本必须将其转换为
所有者 - rwx 组 - r-x 其他人 - r-x
答案 0 :(得分:2)
导入os
permissionDict ={
'access':{
'0':('---'),
'1':('--x'),
'2':('-w-'),
'3':('-wx'),
'4':('r--'),
'5':('r-x'),
'6':('rw-'),
'7':('rwx')
},
'roles':{
0:'owner',
1:'group',
2:'other'
}
}
fname = input('Enter a file Name : ')
if os.path.exists(fname):
permissionOctal = oct(os.lstat(fname).st_mode)[-3:] #645
for role,octal in enumerate(permissionOctal): # [(0,6) , (1,4) , (2,5)]
print(permissionDict['roles'][role],permissionDict['access'][octal])
else:
print('File not found..!')
<强> RESULT 强>
输入文件名:/ tmp
owner rwx
group rwx
other rwx
输入文件名:/ etc / shadow
owner rw-
group r--
other ---
答案 1 :(得分:0)
def octal_to_string(octal):
result = ""
value_letters = [(4, "r"), (2, "w"), (1, "x")]
for permissions in [int(n) for n in str(octal)]:
for value, letter in value_letters:
if permissions >= value: #
result += letter
permissions -= value #
else:
result += "-"
return result