Skip to content

Commit

Permalink
adds error handling and writes a tad safer
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank M. Taylor committed Sep 19, 2022
1 parent 84ddfcc commit bd78fcf
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions xlstojson.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#!/usr/bin/python3.10
import json
import os.path
import json, os, sys
from xlsx_reader import get_workbook, get_workbook_data

def main():
''' The CLI / output task. '''
filename = input("Enter the path to the filename -> ")
if os.path.isfile(filename):
workbook = get_workbook(filename)
workbook_data = get_workbook_data(workbook)
output = \
open((filename.replace("xlsx", "json")).replace("xls", "json"), "w+", encoding="utf-8")
output.write(json.dumps(workbook_data, sort_keys=True, indent=2, separators=(',', ": ")))
output.close()
print (f"{output.name} was created")
source_file = input("Enter the path to the filename -> ")
if os.path.isfile(source_file):
pathname = os.path.splitext(source_file)
file_name = pathname[0].split('/')[-1]
try:
output_file_name = file_name + '.json'
workbook = get_workbook(source_file)
workbook_data = get_workbook_data(workbook)
with open(output_file_name, 'w+', encoding="utf-8") as output_file:
output_file.write(json.dumps(workbook_data, sort_keys=True, indent=2, separators=(",", ": ")))
print (f"{output_file.name} was created")
except Exception as error:
print("some error occured")
print(error)
sys.exit(2)
else:
print ("Sorry, that was not a valid filename")

Expand Down

0 comments on commit bd78fcf

Please sign in to comment.