Most pythonic way to convert a string to a octal number

后端 未结 2 687
我寻月下人不归
我寻月下人不归 2020-12-20 11:47

I am looking to change permissions on a file with the file mask stored in a configuration file. Since os.chmod() requires an octal number, I need to convert a string to an o

2条回答
  •  暖寄归人
    2020-12-20 12:28

    Have you just tried specifying base 8 to int:

    num = int(your_str, 8)
    

    Example:

    s = '644'
    i = int(s, 8) # 420 decimal
    print i == 0644 # True
    

提交回复
热议问题