Wanderer
Wanderer

Reputation: 1197

Saving multiple files on Colab - cp: cannot stat 'path': No such file or directory

I trained several models using Keras on Google Colab and I want to save them to my Drive. All files are '.h5' files.

First, I mounted my drive

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

Then I tried to save the models using

import glob, os
os.chdir("/content/")
for file in glob.glob("*.h5"):
    path = "/content/"+file
    !cp -r path "/content/gdrive/My Drive/models"

But I keep getting this error:

cp: cannot stat 'path': No such file or directory

I tried using from pathlib import Path like this

from pathlib import Path
import glob, os
os.chdir("/content/")
for file in glob.glob("*.h5"):
    path = "/content/"+file
    !cp -r Path(path) "/content/gdrive/My Drive/models"

But it did not work and I got this error

/bin/bash: -c: line 0: syntax error near unexpected token (' /bin/bash: -c: line 0: cp -r Path(path) "/content/gdrive/My Drive/models"'

What can I do?

Thank you.

Upvotes: 0

Views: 2106

Answers (2)

skon7
skon7

Reputation: 379

i used another package called shutil it's efficient for doing this task.

import shutil
shutil.copy(file_source_path, file_dest_path

Upvotes: 0

Wade Wang
Wade Wang

Reputation: 690

Try add curly braces on the variable, here is path, and remove the double quotes on the destination path, so the final command is !cp -r {path} /content/gdrive/My Drive/models

Upvotes: 1

Related Questions