change to absolute path
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
"""\
|
||||
Material database.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
__author__ = "Daniel Weschke"
|
||||
__copyright__ = "Copyright 2019 Daniel Weschke"
|
||||
__credits__ = ["Daniel Weschke"]
|
||||
@@ -21,34 +24,34 @@ EPILOG = """\
|
||||
"""
|
||||
|
||||
|
||||
def read_dir(dir, exclude=[]):
|
||||
def read_dir(directory, exclude=None):
|
||||
"""\
|
||||
Read all files in directory as list
|
||||
"""
|
||||
from os import listdir
|
||||
|
||||
directory = os.path.join(os.path.abspath(os.path.dirname(__file__)), directory)
|
||||
result = []
|
||||
for data in listdir(dir):
|
||||
for data in os.listdir(directory):
|
||||
file_name = data.split('.')[0]
|
||||
if file_name not in exclude:
|
||||
if exclude is not None and file_name not in exclude:
|
||||
result.append(file_name)
|
||||
return result
|
||||
|
||||
|
||||
def print_list(list):
|
||||
def print_list(data_list):
|
||||
"""\
|
||||
Print list
|
||||
"""
|
||||
for data in list:
|
||||
for data in data_list:
|
||||
print(data.title())
|
||||
|
||||
|
||||
def read_file(input_file):
|
||||
def read_file(filename):
|
||||
"""\
|
||||
Read file as string
|
||||
"""
|
||||
filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
|
||||
try:
|
||||
with open(input_file) as data_file:
|
||||
with open(filename) as data_file:
|
||||
return data_file.read()
|
||||
except (OSError, IOError) as err:
|
||||
print(str(err))
|
||||
@@ -105,7 +108,6 @@ def main():
|
||||
"""\
|
||||
Main function
|
||||
"""
|
||||
from os import path
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
@@ -145,11 +147,11 @@ def main():
|
||||
print(args)
|
||||
|
||||
if args.command == 'list':
|
||||
list = read_dir("./data", "INFO")
|
||||
print_list(list)
|
||||
data_list = read_dir("data", "INFO")
|
||||
print_list(data_list)
|
||||
|
||||
elif args.command == 'get':
|
||||
filename = path.join('data', args.materialname + '.json')
|
||||
filename = os.path.join('data', args.materialname.lower() + '.json')
|
||||
data = read_file(filename)
|
||||
if args.debug:
|
||||
print("filename: " + filename)
|
||||
@@ -171,5 +173,4 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
sys.exit(main())
|
||||
|
||||
Reference in New Issue
Block a user