python设置文件只读与取消文件只读

您所在的位置:网站首页 将文件属性设置为只读的操作步骤 python设置文件只读与取消文件只读

python设置文件只读与取消文件只读

2024-07-10 08:20| 来源: 网络整理| 查看: 265

日期:2023年3月3日 作者:Commas 签名:(ง •_•)ง 积跬步以致千里,积小流以成江海…… 注释:如果您觉得有所帮助,帮忙点个赞,也可以关注我,我们一起成长;如果有不对的地方,还望各位大佬不吝赐教,谢谢^ - ^ 1.01365 = 37.7834;0.99365 = 0.0255 1.02365 = 1377.4083;0.98365 = 0.0006

文章目录 一、使用os.chmod来设置文件只读属性二、关于权限代码mode的解读

在这里插入图片描述

一、使用os.chmod来设置文件只读属性

我们可以使用 os 模块中的chmod(全程:change mode)来设置或取消文件的只读属性。

完整语法:

def chmod(path: FileDescriptorOrPath, mode: int,*, dir_fd: int | None = None, follow_symlinks: bool = True) -> None: ...

常见语法:

def chmod(path: FileDescriptorOrPath, mode: int) path:需要修改只读属性的文件路径或目录路径;mode:权限代码,由八进制数字(octat)组成。如0o444表示只读的权限,0o666表示“读写”的权限,0o777表示读写执行的权限等等。

使用方法:

引入os模块;使用os.chmod()方法去设置或取消文件的只读属性;将文件路径和权限代码传递os.chmod()方法

示例如下:

import os # set file as read-only # 4 = read(4) os.chmod('readonly.txt', 0o444) # cancel read-only property # 6 = read(4) + write(2) os.chmod('readonly.txt', 0o666) # set file as read-write-execute # 7 = read(4) + write(2) + execute(1) os.chmod('readonly.txt', 0o777)

当然,您也可以这样写:

import os,stat # (1)set file as read-only # 4 = read(4) # os.chmod('readonly.txt', 0o444) os.chmod('readonly.txt', stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH) # (2)cancel read-only property # 6 = read(4) + write(2) # os.chmod('readonly.txt', 0o666) os.chmod('readonly.txt', stat.S_IWUSR + stat.S_IRUSR + stat.S_IWGRP + stat.S_IRGRP + stat.S_IWOTH + stat.S_IROTH) # (3)set file as read-write-execute # 7 = read(4) + write(2) + execute(1) # os.chmod('readonly.txt', 0o777) os.chmod('readonly.txt', stat.S_IXUSR + stat.S_IWUSR + stat.S_IRUSR + stat.S_IXGRP + stat.S_IWGRP + stat.S_IRGRP + stat.S_IXOTH + stat.S_IWOTH + stat.S_IROTH) # 或 os.chmod('readonly.txt', stat.S_IRWXU + stat.S_IRWXG + stat.S_IRWXO) # 我们可以打印出来看看 # 0o444 print(oct(stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH)) # 0o666 print(oct(stat.S_IWUSR + stat.S_IRUSR + stat.S_IWGRP + stat.S_IRGRP + stat.S_IWOTH + stat.S_IROTH)) # 0o777 print(oct(stat.S_IXUSR + stat.S_IWUSR + stat.S_IRUSR + stat.S_IXGRP + stat.S_IWGRP + stat.S_IRGRP + stat.S_IXOTH + stat.S_IWOTH + stat.S_IROTH)) print(oct(stat.S_IRWXU + stat.S_IRWXG + stat.S_IRWXO))

结果输出:

0o444 0o666 0o777 0o777

二、关于权限代码mode的解读

在这里插入图片描述

如上图所示,文件调用权限分为三级 :

文件所有者(Owner/Users)用户组用户(Group Users)其它用户(Other Users)

其中,每级权限代码范围:1~7。

符号含义:

R:读,Read的缩写,八进制值为 4;W:写,Write的缩写,八进制值为 2;X:执行,Execute的缩写,八进制值为 1;

如:0o444表示只读的权限,0o666表示“读写”的权限,0o777表示读写执行的权限等等;

文件所有者(User)的权限:

权限代码英文名说明stat.S_IXUSRExecute User拥有者具有执行权限0o100stat.S_IWUSRWrite User拥有者具有写权限0o200stat.S_IRUSRRead User拥有者具有读权限0o400stat.S_IRWXURead Write Execute User拥有者有全部权限(权限掩码)0o700

文件用户组(Group)的权限:

权限代码英文名说明stat.S_IXGRPExecute Group组用户有执行权限0o010stat.S_IWGRPWrite Group组用户有写权限0o020stat.S_IRGRPRead Group组用户有读权限0o040stat.S_IRWXGRead Write Execute Group组用户有全部权限(权限掩码)0o070

文件其他用户(Other)的权限:

权限代码英文名说明stat.S_IXOTHExecute Other其他用户有执行权0o001stat.S_IWOTHWrite Other其他用户有写权限0o002stat.S_IROTHRead Other其他用户有读权限0o004stat.S_IRWXORead Write Execute Other其他用户有全部权限(权限掩码)0o007

其它:

权限代码说明stat.S_ISVTX目录里文件目录只有拥有者才可删除更改0o1000stat.S_ISGID执行此文件其进程有效组为文件所在组0o2000stat.S_ISUID执行此文件其进程有效用户为文件所有者0o4000stat.S_IREADwindows下设为只读stat.S_IWRITEwindows下取消只读

我的微信公众号【会飞的小猴子】,等你来关注哦 ^ - ^

参考文章: 1、《Linux chmod命令》

版权声明:本文为博主原创文章,如需转载,请给出: 原文链接:https://blog.csdn.net/qq_35844043/article/details/129292978



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3