如何从Jupyter Notebook下载所有文件和文件夹层次结构? [英] How to download all files and folder hierarchy from Jupyter Notebook?

查看:3001
本文介绍了如何从Jupyter Notebook下载所有文件和文件夹层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想如图所示从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?

注意:此Jupyter笔记本是由在线课程老师创建的,因此它不是从我本地的Acaconda应用中打开的,而是从在线课程网页中打开的.下载是为了将来需要时刷新内存.

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.

推荐答案

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)

要使用该功能,只需在要下载的根文件夹中创建一个新的.ipynb笔记本.然后将上面的代码复制并粘贴到第一个单元格中,然后运行它.

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.

完成后-您将在同一文件夹中看到一个tar文件,其中包含所有文件和子文件夹.

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

这篇关于如何从Jupyter Notebook下载所有文件和文件夹层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆