擅长:python、mysql、java
<p>以下是解决方案:</p>
<p>将八进制字符串“777”转换为十进制数字511</p>
<pre><code>dec_num = int(oct_string, 8) # "777" -> 511
</code></pre>
<p>你担心os.chmod()?我们试试看!</p>
<pre><code>os.chmod("file", 511) # it works! -rwxrwxrwx.
os.chmod("file", 0777) # it works! -rwxrwxrwx.
os.chmod("file", int("2777",8)) # it works! -rwxrwsrwx. the number is 1535!
</code></pre>
<p>它证明了python中的chmod<code>can accept decimal</code>和<code>decimal can used as octal</code>!</p>
<hr/>
<p>八进制就够了,因为如果你试着</p>
<pre><code>print dec_num == 0777 # True
</code></pre>
<p>然后将十进制数511转换为八进制<code>string</code>“0777”</p>
<pre><code>oct_num = oct(dec_num) # 511 -> "0777" , it is string again.
</code></pre>
<p>注意没有0777号,但只有10月字符串“0777”或12月数字
在python中如果你写了0777号,它会自动把0777号翻译成511号,在这个过程中不存在0777号。</p>
<p>摘要</p>
<pre><code>dec_num = int(oct_string, 8)
oct_num = oct(int(oct_string,8))
print dec_num # 511
print dec_num == 0777 # True ! It is actually 511, just like dec_num == 511
print oct_num # 0777
print oct_num == 0777 # Flase! Because it is a string!
</code></pre>