How to convert .ipynb into .py files

anish jain
Nov 2, 2020

--

import json 
import os
file_name = input("Enter the path of '.ipynb' file:")
save_file_name = file_name.split("\\")[-1].split(".")[0]
print(save_file_name)# Opening JSON file
f = open(file_name)

# returns JSON object as
# a dictionary
data = json.load(f)
total_cells = len(data['cells'])code = []

# Iterating through the json
# list
for cell in data['cells']:
if cell['cell_type'] == "code":
for source in cell["source"]:
if not source.endswith("\n"):
source=source+"\n"
#print(source)
code.append(source)
# Closing file
f.close()
'''
for c in code:
if not c.endswith("\n"):
print(c)
'''pyfile = open(f"{save_file_name}.txt","w+")
for c in code:
pyfile.write(c)
pyfile.close()
os.rename(f"{save_file_name}.txt",f"{save_file_name}.py")
print("Done Conversion")

--

--

anish jain
anish jain

Written by anish jain

like to create things,and some time to break them to get indepth knowledge,stronly believe in learning never exhaust the mind

No responses yet