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

▼魔方 西西 提交于 2019-12-02 23:51:11
Jason
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.

The above posted answer mostly works but its copying links instead of the files the links point to. If you add dereference=True as an argument to tarfile.open you will get the files themselves.

    tar = tarfile.open(tar_file_name, 'w', dereference=True)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!