【问题标题】:Converting all files in folder to .py files将文件夹中的所有文件转换为 .py 文件
【发布时间】:2017-08-21 17:28:08
【问题描述】:

我想知道他们是否无论如何都要通过并复制文件夹中的每个 .ipynb 文件,然后将该文件更改为 .py。我想保留 .ipynb 文件,但我也想拥有一个 .py 文件。我知道如何手动执行此操作,但想要一种为指定目录中的每个文件自动执行此操作的方法。

【问题讨论】:

  • 检查this,我想会有帮助

标签: python


【解决方案1】:

只需在该文件夹内的 Cmd 提示符下尝试此操作即可。

jupyter nbconvert *.ipynb --to script

将所有 .ipynb 文件转换为 .py 并保留所有 .ipynb 文件。
希望对你有帮助

更多详情,也请查看此链接Save .ipynb to .py automatically on saving notebook

【讨论】:

    【解决方案2】:

    find . -name "*.py" -exec ipython nbconvert --to=python {} \; 应该可以在 Linux 上运行。

    【讨论】:

      【解决方案3】:
      import os
      
      directory = 'ipynb PATH'
      
      for subdir, dirs, files in os.walk(directory):
          for file in files:
              if os.path.splitext(file)[1] == '.ipynb':
                  exe_command = 'jupyter nbconvert "{}" --to python'.format(''.join(os.path.join(subdir, file)))
                  
                  # print the command`enter code here`
                  # print (exe_command)
                  
                  os.system(exe_command)
                  
                  # Delete the file 
                  # os.remove(''.join(os.path.join(subdir, file)))
              else:
                  continue
      

      【讨论】:

      • 将一个目录下的所有.ipynb文件转换成.py 可以删除转换后的文件,之前需要pip install nbconvert
      • 惊人的解决方案 - 帮助将我所有的 ipynb 转换为 py
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多