Featured image of post 【Python 檔案處理 #4】在 python 中利用 os.chmod 更改檔案的權限 (chmod 777)

【Python 檔案處理 #4】在 python 中利用 os.chmod 更改檔案的權限 (chmod 777)

前言

在 linux 系統中,我們會經常使用 chmod 來做檔案權限的更改,
我們同樣也能在 python 中使用 os.chmod 達到一樣的效果。

範例程式碼

import os 

with open("test.txt", 'w') as f_out:    
    f_out.write("test")

# 可利用註解查看效果
os.chmod("test.txt", 0o777)

結果

修改前與修改後的檔案變化如下,我們可以看見檔案權限的變化。

Reference

https://www.runoob.com/python/os-chmod.html