无法打开文件在 Colab 中没有这样的文件或目录

问题描述 投票:0回答:2

目前我正在尝试在鸟类数据集上实施

taoxugit/AttnGAN
工作。我已将项目上传到驱动器并将其导入 Colab。

    from google.colab import drive
    drive.mount('/content/drive')

     %cd /content/drive/MyDrive

!unrar x "/content/drive/MyDrive/fashiongen/AttnGAN-master1.rar" 

%cd /content/drive/MyDrive/AttnGAN-master1/AttnGAN-master 

但是当我尝试在行下运行时,我得到以下错误

!python pretrain_DAMSM.py --cfg cfg/DAMSM/bird.yml --gpu  python3: can't open file 'pretrain_DAMSM.py': [Errno 2] No such file or directory

我可以在

/content/drive/MyDrive/AttnGAN-master1/AttnGAN-master/code/pretrain_DAMSM.py
中看到文件 但不知道为什么我会收到此错误。我也遇到了 bird.yml 的错误

    import os
    os.chdir("/content/drive/MyDrive/AttnGAN-master1/AttnGAN-master")
    os.getcwd()

我用这段代码来设置路径。但没有用仍然出错。现在我进入了完整路径。

    !python /content/drive/MyDrive/AttnGAN-master1/AttnGAN- 
     master/code/pretrain_DAMSM.py --cfg /content/drive/MyDrive/AttnGAN- 
     master1/AttnGAN-master/code/cfg/DAMSM/bird.yml --gpu 0 

但现在又出现另一个错误

FileNotFoundError: [Errno 2] 没有这样的文件或目录:

'../data/birds/CUB_200_2011/bounding_boxes.txt'

我可以看到指定文件夹中的文本文件,但不知道为什么colab找不到任何文件。

python google-colaboratory
2个回答
0
投票

您需要提供文件的完整路径,因为

cd
不会持续存在。

所以它会是这样的。

!python /content/drive/MyDrive/AttnGAN-master1/AttnGAN-master/code/pretrain_DAMSM.py --cfg <full path to >/bird.yml --gpu

您还必须正确获取

bird.yml
文件的路径。我的建议是使用 Colab 中的文件视图找到它,然后右键单击它并执行
Copy path
.


0
投票

我最近做了同样的实验,有了一点感悟。

问题是你没有先进入正确的目录,导致程序无法根据相对路径找到对应的文件。应该像下面这样使用:

%cd /content/drive/MyDrive/AttnGAN-master1/AttnGAN-master/code
!python pretrain_DAMSM.py --cfg cfg/DAMSM/bird.yml --gpu 0
© www.soinside.com 2019 - 2024. All rights reserved.