访问 google colab 上的文件
Accessing files on google colab
我正在使用 Google Colaboratory IPython 进行风格转换,在按 运行 安装我的驱动器后:
from google.colab import drive
drive.mount('/drive')
它已挂载,所以我尝试 cd 进入一个目录,显示密码和 ls 但它没有显示正确的密码
!cd "/content/drive/My Drive/"
!pwd
!ls
但它不会进入给定的目录,它只会进入 'content/'
同样,当我尝试在我的代码中使用“load_image() 函数访问一些图像时,如下所示
def load_image(img_path, max_size=400, Shape=None):
image = Image.open(img_path).convert('RGB')
if max(image.size) > max_size:
size = max_size
else:
size = max(image.size)
if shape is not None:
size = shape
in_transform = transforms.Compose([transforms.Resize(size),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))])
image = in_transform(image)[:3,:,:].unsqueeze(0)
return image
#load image content
content = load_image('content/drive/My Drive/uche.jpg')
style = load_image('content/drive/My Drive/uche.jpg')
但是当我尝试从目录加载图像时,这段代码抛出错误:
FileNotFoundError: [Errno 2] 没有那个文件或目录: 'content/drive/My Drive/uche.jpg'
简短回答:要更改工作目录,请使用 %cd
或 os.chdir
而不是 !cd
。
背后的故事是 !
命令在子 shell 中执行,具有来自 Python 进程 运行 您的代码的独立工作目录。但是,您想要的是更改 Python 进程的工作目录。这就是 os.chdir
的作用,%cd
是在笔记本中使用的方便的别名。
放在一起,我想你想写:
from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/My\ Drive
我正在使用 Google Colaboratory IPython 进行风格转换,在按 运行 安装我的驱动器后:
from google.colab import drive
drive.mount('/drive')
它已挂载,所以我尝试 cd 进入一个目录,显示密码和 ls 但它没有显示正确的密码
!cd "/content/drive/My Drive/"
!pwd
!ls
但它不会进入给定的目录,它只会进入 'content/'
同样,当我尝试在我的代码中使用“load_image() 函数访问一些图像时,如下所示
def load_image(img_path, max_size=400, Shape=None):
image = Image.open(img_path).convert('RGB')
if max(image.size) > max_size:
size = max_size
else:
size = max(image.size)
if shape is not None:
size = shape
in_transform = transforms.Compose([transforms.Resize(size),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))])
image = in_transform(image)[:3,:,:].unsqueeze(0)
return image
#load image content
content = load_image('content/drive/My Drive/uche.jpg')
style = load_image('content/drive/My Drive/uche.jpg')
但是当我尝试从目录加载图像时,这段代码抛出错误:
FileNotFoundError: [Errno 2] 没有那个文件或目录: 'content/drive/My Drive/uche.jpg'
简短回答:要更改工作目录,请使用 %cd
或 os.chdir
而不是 !cd
。
背后的故事是 !
命令在子 shell 中执行,具有来自 Python 进程 运行 您的代码的独立工作目录。但是,您想要的是更改 Python 进程的工作目录。这就是 os.chdir
的作用,%cd
是在笔记本中使用的方便的别名。
放在一起,我想你想写:
from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/My\ Drive