From 6232d48a54ff059e2e7897da3e98fc73d8f942fc Mon Sep 17 00:00:00 2001 From: Google Colaboratory Team Date: Thu, 10 Oct 2024 13:35:09 -0700 Subject: [PATCH] No public description PiperOrigin-RevId: 684560175 --- google/colab/files.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/google/colab/files.py b/google/colab/files.py index 47567a7c..9ee5aa4e 100644 --- a/google/colab/files.py +++ b/google/colab/files.py @@ -57,11 +57,14 @@ def upload_file(filename): ) -def upload(): +def upload(target_dir=''): """Render a widget to upload local (to the browser) files to the kernel. Blocks until the files are available. + Args: + target_dir: If provided, corresponds to the directory to write the files to. + Returns: A map of the form {: } for all uploaded files. """ @@ -71,11 +74,16 @@ def upload(): local_filenames = dict() uploaded_files_with_updated_names = dict() + # If there was a specified target directory, create it. + if target_dir: + _os.makedirs(target_dir, exist_ok=True) + for filename, data in uploaded_files.items(): - local_filename = local_filenames.get(filename) + complete_filename = _os.path.join(target_dir, filename) + local_filename = local_filenames.get(complete_filename) if not local_filename: - local_filename = _get_unique_filename(filename) - local_filenames[filename] = local_filename + local_filename = _get_unique_filename(complete_filename) + local_filenames[complete_filename] = local_filename print( 'Saving {filename} to {local_filename}'.format( filename=filename, local_filename=local_filename