编写python代码转换linux读,写和执行八进制格式的字符串

db2dz4w8  于 2023-05-27  发布在  Python
关注(0)|答案(9)|浏览(340)

Linux系统中的文件权限分为三组,每组三个权限:为所有者、组和其他人读取、写入和执行。这三个值中的每一个都可以表示为八进制数,对每个权限求和,其中4对应于读取,2对应于写入,并且1对应于执行。也可以使用字母r、w和x或-(如果未授予权限)编写字符串。例如:640为所有者读/写,组读,其他无权限;转换为字符串,它将是:“rw-r-----”755是对所有者的读/写/执行,以及对组和其他的读/执行;转换为字符串,它将是:“rwxr-xr-x”填写空格,使代码将八进制格式的权限转换为字符串格式。

def octal_to_string(octal):
  result = ""
  value_letters = [(4,"r"),(2,"w"),(1,"x")]
  # Iterate over each of the digits in octal
  for ___ in [int(n) for n in str(octal)]:
      # Check for each of the permissions values
      for value, letter in value_letters:
          if ___ >= value:
              result += ___
              ___ -= value
          else:
              ___
  return result

print(octal_to_string(755)) # Should be rwxr-xr-x
print(octal_to_string(644)) # Should be rw-r--r--
print(octal_to_string(750)) # Should be rwxr-x---
print(octal_to_string(600)) # Should be rw-------

请帮我解决这个问题

vuv7lop3

vuv7lop31#

def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for i in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if i >= value:
                result += letter
                i -= value
            else:
                result += "-"
    return result
    
print(octal_to_string(755)) # Should be rwxr-xr-x
print(octal_to_string(644)) # Should be rw-r--r--
print(octal_to_string(750)) # Should be rwxr-x---
print(octal_to_string(600)) # Should be rw-------
blmhpbnm

blmhpbnm2#

def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for y in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if y >= value:
                result += letter
                y-= value
            else:
                result+="-"
    return result

print(octal_to_string(755))

rwxr-xr-x

t5zmwmid

t5zmwmid3#

def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for x in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if x >= value:
                result += letter
                x -= value
            else:
                result +="-"
    return result
    
print(octal_to_string(755)) # Should be rwxr-xr-x
print(octal_to_string(644)) # Should be rw-r--r--
print(octal_to_string(750)) # Should be rwxr-x---
print(octal_to_string(600)) # Should be rw-------
w9apscun

w9apscun4#

既然你的问题是关于"Fill in the blanks to ....a string format."
我们开始吧

def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    
    for num in [int(n) for n in str(octal)]:
        
        for value, letter in value_letters:
            if num >= value:       #checks if num not exceeds value of value_letters
                result += letter   #adds coherent letter to result 
                num -= value       #substracts for new value to check next permission
            else:
                result += "-"
    return result
    
print(octal_to_string(755)) # Should be rwxr-xr-x
print(octal_to_string(644)) # Should be rw-r--r--
print(octal_to_string(750)) # Should be rwxr-x---
print(octal_to_string(600)) # Should be rw-------

在这个剧本里,
for num in [int(n) for n in str(octal)]:
做四件事; n迭代每个字符串的八进制数,int(n)将这些str值转换为整数,列表解析将获得的整数转换为列表,最后num迭代列表解析中的每个整数。
而且,由于给定的value_letters是元组,因此for value, letter in value_letters:分别迭代for value和letter。
希望我能帮到你。:D

8nuwlpux

8nuwlpux5#

答案是

def octal_to_string(octal):
 result = ""
 value_letters = [(4,"r"),(2,"w"),(1,"x")]
 # Iterate over each of the digits in octal
 for i in [int(n) for n in str(octal)]:
    # Check for each of the permissions values
    for value, letter in value_letters:
        if i >= value:
            result += letter
            i -= value
        else:
            result += '-'
 return result

解释:
考虑八进制= 755

# Iterate over each of the digits in octal
  for i in [int(n) for n in str(octal)]:

在这里,它将创建如下列表
[7,5,5]
在注解中,我展示了循环如何在7

value_letters = [(4,"r"),(2,"w"),(1,"x")]
for value, letter in value_letters:     # 1st loop | 2nd .. | ...
 if i >= value:                         # i=7 , 7>4  | i=3 , 3>2   | i=1 , 1=1 
            result += letter            # result = r | result = rw | result = rwx
            i -= value                  # 7-4=3      | 3-2=1       | 1-1=0
        else:
            result += '-'                                         # | i = 0 , 0<1
 return result                                                    # | result = rwx-

则它将对创建的列表中的每个数字执行相同的过程
我希望答案是明确的。

p1tboqfb

p1tboqfb6#

考虑使用stat.filemode
https://docs.python.org/3/library/stat.html#stat.filemode

hivapdat

hivapdat7#

  • 更简单的方法。
def octal_to_string(octal):
    permission = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]
    result = ""
    # Iterate over each of the digits in octal
    for ___ in [int(n) for n in str(octal)]:
        result += permission[___]
    return result

print(octal_to_string(755)) 
print(octal_to_string(644)) 
print(octal_to_string(750)) 
print(octal_to_string(600))

  • 按照你的逻辑。
def octal_to_string(octal):
   result = ""
   value_letters = [(4,"r"),(2,"w"),(1,"x")]
   # Iterate over each of the digits in octal
   for ___ in [int(n) for n in str(octal)]:
      # Check for each of the permissions values
      for value, letter in value_letters:
          if ___ >= value:
               result += letter
               ___ -= value
          else:
               result += "-"
   return result

print(octal_to_string(755))
print(octal_to_string(644))
print(octal_to_string(750))
print(octal_to_string(600))

pinkon5k

pinkon5k8#

def octal_to_string(octal):
 result = ""
 value_letters = [(4,"r"),(2,"w"),(1,"x")]
 # Iterate over each of the digits in octal
 for i in [int(n) for n in str(octal)]:
    # Check for each of the permissions values
    for value, letter in value_letters:
        if i >= value:
            result += letter
            i -= value
        else:
            result += '-'
 return result
n7taea2i

n7taea2i9#

def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for x in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if x >= value:
                result += letter
                x -= value
            else:
                result += "-"
    return result

相关问题