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