From b215cfcabd38627d4f9b0229d4100ebd677f7b73 Mon Sep 17 00:00:00 2001 From: Siddhesh Bhupendra Kuakde <65951872+SiddheshKukade@users.noreply.github.com> Date: Mon, 10 Oct 2022 18:47:49 +0530 Subject: [PATCH] Create json2yaml.py --- scripts/JSON-to-YAML Converter/json2yaml.py | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 scripts/JSON-to-YAML Converter/json2yaml.py diff --git a/scripts/JSON-to-YAML Converter/json2yaml.py b/scripts/JSON-to-YAML Converter/json2yaml.py new file mode 100644 index 0000000..9323e69 --- /dev/null +++ b/scripts/JSON-to-YAML Converter/json2yaml.py @@ -0,0 +1,35 @@ +import json +import os +import sys +import yaml + +# Checking there is a file name passed +if len(sys.argv) > 1: + # Opening the file + if os.path.exists(sys.argv[1]): + source_file = open(sys.argv[1], "r") + source_content = json.load(source_file) + source_file.close() + # Failikng if the file isn't found + else: + print("ERROR: " + sys.argv[1] + " not found") + exit(1) +# No file, no usage +else: + print("Usage: json2yaml.py [target_file.yaml]") + +# Processing the conversion +output = yaml.dump(source_content) + +# If no target file send to stdout +if len(sys.argv) < 3: + print(output) +# If the target file already exists exit +elif os.path.exists(sys.argv[2]): + print("ERROR: " + sys.argv[2] + " already exists") + exit(1) +# Otherwise write to the specified file +else: + target_file = open(sys.argv[2], "w") + target_file.write(output) + target_file.close()