3

I was wondering if their is anyway to go through and make copies of each .ipynb file in a folder and then change that file to .py. I want to keep the .ipynb files but I would like to have a .py file as well. I know how to do it manually but would like a way to do it automatically for each file in a specified directory.

Cannon
  • 309
  • 4
  • 19
  • check [this](https://stackoverflow.com/questions/37797709/convert-json-ipython-notebook-ipynb-to-py-file) out, I think it'll help – jacoblaw Aug 21 '17 at 17:34

3 Answers3

4

Just try this at Cmd prompt inside that folder.

jupyter nbconvert *.ipynb --to script

Converts all .ipynb files to .py and retains all .ipynb files as well.
Hope it helps

For more details, Check this link as well Save .ipynb to .py automatically on saving notebook

Karan Kaw
  • 510
  • 1
  • 7
  • 15
3

find . -name "*.py" -exec ipython nbconvert --to=python {} \; should work on Linux.

0x5453
  • 12,753
  • 1
  • 32
  • 61
2
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