如何在Google Colab中挂载Google Drive并获得写入权限?
在Google Colab中挂载Google Drive并获取写入权限的解决方法
问题说明
运行AI模型时遭遇只读文件系统错误,无法写入文件:
Traceback (most recent call last): File "/content/Deep3DFaceRecon_pytorch/test.py", line 72, in <module> opt = TestOptions().parse() # get test options ^^^^^^^^^^^^^^^^^^^^^ File "/content/Deep3DFaceRecon_pytorch/options/base_options.py", line 167, in parse self.print_options(opt) File "/content/Deep3DFaceRecon_pytorch/options/base_options.py", line 115, in print_options with open(file_name, 'wt') as opt_file: ^^^^^^^^^^^^^^^^^^^^^ OSError: [Errno 30] Read-only file system: './checkpoints/facerecon_20230425/test_opt.txt'
尝试在已复制的文件夹内创建文件时同样报错:
!touch ./checkpoints/facerecon_20230425/test_opt.txt
错误日志:
touch: cannot touch './checkpoints/facerecon_20230425/test_opt.txt': Read-only file system
已执行的操作:
!git clone https://github.com/sicxu/Deep3DFaceRecon_pytorch.git %cd Deep3DFaceRecon_pytorch ... !mkdir checkpoints !cp -r ../drive/MyDrive/Deep3D/facerecon_20230425 checkpoints/ ...
解决步骤
1. 正确挂载Google Drive
执行以下代码完成挂载,按弹出的授权链接提示完成登录验证:
from google.colab import drive drive.mount('/content/drive')
挂载完成后,Google Drive的内容会映射到 /content/drive/MyDrive/ 路径,该路径默认拥有完整读写权限。
2. 调整文件操作方式
方案一:直接操作Drive内的文件夹
修改模型代码中的输出路径,直接指向Drive内的目标文件夹(如 /content/drive/MyDrive/Deep3D/facerecon_20230425/),跳过本地复制步骤,直接在Drive中完成写入操作,从根源避免权限问题。
方案二:修复本地文件夹权限
如果必须将文件复制到Colab本地目录,复制后执行以下命令修改文件夹权限:
!chmod -R 777 ./checkpoints/facerecon_20230425
该命令会赋予文件夹及内部文件读写执行权限,后续写入操作即可正常进行。
3. 验证权限有效性
完成挂载或权限修改后,执行以下命令测试写入功能:
# 测试Drive路径写入 !touch /content/drive/MyDrive/Deep3D/test_write.txt # 测试本地文件夹写入(若用方案二) !touch ./checkpoints/facerecon_20230425/test_write.txt
无报错则说明权限配置正常。
内容的提问来源于stack exchange,提问作者Megidd
相关产品推荐
相关产品推荐

