当前位置:首页 > 操作系统 > MacOs

Python3 解决 MacOS Big Sur 下 OSError: [Errno 30] Read-only file system 报错

MacOS 升级到 Big Sur 后,使用 Python3 的 os 模块发现文件写不到磁盘。会报 OSError: [Errno 30] Read-only file system 的错误。

一、分析错误

既然有错误,我们就分析错误!

经过了解,是在 Mac OS10.11 之后,苹果公司为了提高系统环境安全,引入了一个内核保护措施 SIP(System Integrity Protection,系统完整性保护),又称Rootless mode 机制。

SIP 机制下,系统默认会锁定 /system/sbin/usr 这三个目录,即使切换到 root 用户也只能查看,不能进行其他操作。

SIP 可以有效地防止恶意程序对电脑进行破坏,所以平时建议保持开启状态。

需要更详细的了解请查看https://eclecticlight.co/2020/06/25/big-surs-signed-system-volume-added-security-protection

解决问题

了解了问题接下来,就着手解决问题,既然是 SIP 导致的,那我们就从 SIP 开始。

查看 SIP 状态。

<code>MacBook:~ zhangyi$ csrutil status
System Integrity Protection status: open.
</code>

发现 SIP 处在开启状态,那就将 SIP 禁用。

<code>MacBook:~ zhangyi$ csrutil disable
csrutil: This tool needs to be executed from Recovery OS.
</code>

当我输入命令后发现这条命令只能在恢复模式中执行。

。。。

那该进入苹果的恢复模式呢?

首先重启电脑,苹果图标亮的时候,同时按住 Command+R 进入恢复模式。

进入到了恢复页面,打开终端。

【顶部菜单栏】->【实用工具】->【打开终端】
Python3 解决 MacOS Big Sur 下 OSError: [Errno 30] Read-only file system 报错 - 文章图片
然后执行禁用命令。

<code>csrutil disable 
</code>

Python3 解决 MacOS Big Sur 下 OSError: [Errno 30] Read-only file system 报错 - 文章图片
最后重启就可以了。

<code>reboot
</code>

一般到这步就可以了,如果想要重新开启 SIP 也需要在恢复模式下执行。

<code>csrutil enable
</code>

如果没解决记得试试这个命令。

<code>sudo mount -uw /
</code>

挂载根目录。

但是有可能会权限不足有报错:

<code>mount_apfs: volume could not be mounted: Permission denied
mount: / failed with 66

mount_apfs: volume could not be mounted: Operation not permitted
mount: / failed with 77
</code>

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐