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 状态。

MacBook:~ zhangyi$ csrutil status
System Integrity Protection status: open.

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

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

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

。。。

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

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

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

【顶部菜单栏】->【实用工具】->【打开终端】
在这里插入图片描述
然后执行禁用命令。

csrutil disable 

在这里插入图片描述
最后重启就可以了。

reboot

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

csrutil enable

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

sudo mount -uw /

挂载根目录。

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

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

猜你喜欢

转载自blog.csdn.net/yilovexing/article/details/113760937
今日推荐