Give me a script to save all files in a folder to my google drive in google colab
- User
Give me a script to save all files in a folder to my google drive in google colab
- Assistant
Certainly! In Google Colab, you can use the
google.colablibrary to work with Google Drive easily. The following script will help you save all files from a specified folder in your Colab environment to your Google Drive.- Mount Google Drive - First, you'll need to mount your Google Drive in the Colab environment.
- Copy files - Next, you will copy files from the specific folder to your Google Drive.
Here's a step-by-step guide:
Step 1: Mount Google Drive
Run the following code to mount your Google Drive:
from google.colab import drive drive.mount('/content/drive')You'll get a link to authorize the Colab to access your Google Drive. Follow the link, sign in, and copy the authorization code back to Colab.
Step 2: Copy Files
Use the following script to copy all files from your specified folder to a destination folder in your Google Drive.
import os import shutil # Define the source folder and the destination folder in Google Drive source_folder = '/content/your_folder' # Make sure to replace 'your_folder' with the actual folder name destination_folder = '/content/drive/My Drive/your_destination_folder' # Make sure to replace 'your_destination_folder' with the actual target folder name in Google Drive # Create destination folder if it does not exist if not os.path.exists(destination_folder): os.makedirs(destination_folder) # List all files in the source folder files = os.listdir(source_folder) # Copy each file to the destination folder for file_name in files: full_file_name = os.path.join(source_folder, file_name) if os.path.isfile(full_file_name): shutil.copy(full_file_name, destination_folder) print(f"All files from {source_folder} have been copied to {destination_folder}")Full Example in One Code Block
Putting it all together, here’s the complete script:
from google.colab import drive import os import shutil # Step 1: Mount Google Drive drive.mount('/content/drive') # Step 2: Define the source and destination folders source_folder = '/content/your_folder' # Replace with your folder name in Colab destination_folder = '/content/drive/My Drive/your_destination_folder' # Replace with your destination folder in Google Drive # Step 3: Create destination folder if it does not exist if not os.path.exists(destination_folder): os.makedirs(destination_folder) # Step 4: Copy all files from source to destination folder files = os.listdir(source_folder) for file_name in files: full_file_name = os.path.join(source_folder, file_name) if os.path.isfile(full_file_name): shutil.copy(full_file_name, destination_folder) print(f"All files from {source_folder} have been copied to {destination_folder}")Important Notes
- Ensure to replace
'your_folder'and'your_destination_folder'with the actual names of your folders. - This will copy only files, not sub-folders. If you need to copy folders including nested sub-folders, you can use
shutil.copytree()instead ofshutil.copy().
Feel free to run the script in Colab, and this will transfer the files from your Colab environment to your Google Drive.