Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปลี่ยน Mode (Permission) ของไฟล์ใน Python

ใช้ os.chmod(file, mode) เปลี่ยน permission mode ต้องเป็น octal (ขึ้นต้นด้วย 0o) เช่น 0o777

>>> import os
>>> os.chmod('my_file', 0o777)

หรือใช้ flags จากโมดูล stat (รายละเอียด)

อีกวิธี ใช้ subprocess เรียก chmod:

>>> import subprocess
>>> subprocess.call(['chmod', '0444', 'my_file'])