From b4e922e9c7c8df2d467252b02687db9a2438ad21 Mon Sep 17 00:00:00 2001 From: firley <konrad.firley@student.reutlingen-university.de> Date: Sun, 21 Apr 2024 20:47:43 +0200 Subject: [PATCH] fixed stuff with the convert.py and added new commands so it is easier to understand --- convert.py | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/convert.py b/convert.py index 4076e88..cf1ae53 100644 --- a/convert.py +++ b/convert.py @@ -16,31 +16,38 @@ from os import listdir ############################################################################################################# #like this: /./Users/Lomikukus/Coding/ML_Projekt/machine-learning-services-main/ +# ./ should work 99% of the time +print("hello") +directory = os.fsencode('./') #th -directory = os.fsencode('/./Users/Lomikukus/Coding/ML_Projekt/machine-learning-services-main/') - +print("yes") f = [] #walks over all files and folders that are listed in the folder which u used in path -for (root, dirnames, filenames) in os.walk(directory): #this outputs a list of all roots, directorie names, and filenames + +try: + for (root, dirnames, filenames) in os.walk(directory): + #this outputs a list of all roots, directorie names, and filenames - for file in filenames: #for each file found... - #sprint(file) - pathtofile = os.path.join(root, file) - #print(pathtofile) - filename = os.fsdecode(file) - filenameX = str(filename) - #print(filenameX) - if filename.endswith(".ipynb"): #...it checks if the file ends in .ipynb... - print(os.path.join(root, file)) - print("found") + for file in filenames: #for each file found... + #sprint(file) + pathtofile = os.path.join(root, file) + #print(pathtofile) + filename = os.fsdecode(file) + + if filename.endswith(".ipynb"): #...it checks if the file ends in .ipynb... + print(os.path.join(root, file)) + print("found") - with open(pathtofile, mode="r", encoding="utf-8") as sf: #...if true then they are being corectly converted to a good json format - mynotebook = json.loads(sf.read()) - namejson = "notebook.json" - namejson = os.fsencode(namejson) - newfilepath = os.path.join(root, namejson) - with open(newfilepath, mode="w", encoding="utf-8") as newjson: - json.dump(mynotebook, newjson, ensure_ascii=False, indent=4) - print("converted") + with open(pathtofile, mode="r", encoding="utf-8") as sf: #...if true then they are being corectly converted to a good json format + mynotebook = json.loads(sf.read()) + namejson = "notebook.json" + namejson = os.fsencode(namejson) + newfilepath = os.path.join(root, namejson) + #this part opens a new file in write mode and creates a notebook.json + with open(newfilepath, mode="w", encoding="utf-8") as newjson: + json.dump(mynotebook, newjson, ensure_ascii=False, indent=4) + print("converted") +except: + print("error") \ No newline at end of file -- GitLab