Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách thay đổi permission file bằng Python?


Để đổi permission file, dùng os.chmod(file, mode). mode dạng octal (bắt đầu 0o). Ví dụ set 0o777:

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

Cũng dùng flag từ module stat. Xem: https://docs.python.org/2/library/stat.html

Cách khác dùng subprocess:

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