Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download all files and folder hierarchy from Jupyter Notebook?

If I want to download all of the files and folder hierarchy from Jupyter Notebook as shown in the picture, do you know if there is anyway to do that by simple click other than go to every single file in every folder to open the file and click download hundreds of times?

enter image description here

Note: This Jupyter Notebook is created by the online course teacher, so it's not opened from my local Acaconda app but the online course webpage instead. Downloading is for future memory refreshing whenever needed.

like image 916
Jason Avatar asked Jan 05 '18 23:01

Jason


People also ask

How do you save a Jupyter Notebook folder?

Saving a Jupter notebookThere is a disk icon in the upper left of the Jupyter tool bar. Click the save icon and your notebook edits are saved. It's important to realize that you will only be saving edits you've made to the text sections and to the coding windows. You will NOT be saving the results of running the code.


1 Answers

import os
import tarfile

def recursive_files(dir_name='.', ignore=None):
    for dir_name,subdirs,files in os.walk(dir_name):
        if ignore and os.path.basename(dir_name) in ignore: 
            continue

        for file_name in files:
            if ignore and file_name in ignore:
                continue

            yield os.path.join(dir_name, file_name)

def make_tar_file(dir_name='.', tar_file_name='tarfile.tar', ignore=None):
    tar = tarfile.open(tar_file_name, 'w')

    for file_name in recursive_files(dir_name, ignore):
        tar.add(file_name)

    tar.close()


dir_name = '.'
tar_file_name = 'archive.tar'
ignore = {'.ipynb_checkpoints', '__pycache__', tar_file_name}
make_tar_file(dir_name, tar_file_name, ignore)

To use that, just create a new .ipynb notebook at the root folder, the one that you want to download. Then copy and paste the code above in the first cell and run it.

When it is done - you will see a tar file created in the same folder, which contains all the files and subfolders.

like image 119
Jason Avatar answered Sep 19 '22 14:09

Jason