Automatically get the latest json file if only given a folder as path
This commit is contained in:
parent
4690c6c681
commit
a8a56801a2
1 changed files with 15 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
import os
|
||||
import npyscreen
|
||||
import json
|
||||
import datetime
|
||||
|
@ -65,9 +66,20 @@ class FormApp(npyscreen.NPSAppManaged):
|
|||
def main(*_args):
|
||||
if len(sys.argv) > 1:
|
||||
file_path = pathlib.Path(sys.argv[1])
|
||||
if not file_path.exists():
|
||||
print("Trying to open nonexistent file. Aborting.")
|
||||
sys.exit(0)
|
||||
|
||||
if(sys.argv[1].endswith(".json")):
|
||||
# json file as argument
|
||||
|
||||
if not file_path.exists():
|
||||
print("Trying to open nonexistent file. Aborting.")
|
||||
sys.exit(0)
|
||||
|
||||
elif file_path.is_dir():
|
||||
# we want the latest json file in given folder
|
||||
filesList = [f for f in os.listdir(file_path) if os.path.isfile(os.path.join(file_path, f)) and f.endswith(".json")]
|
||||
filesList.sort()
|
||||
file_path = os.path.join(file_path, filesList[len(filesList) - 1])
|
||||
|
||||
else:
|
||||
file_path = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json')
|
||||
|
||||
|
|
Loading…
Reference in a new issue