dirStuff.findFileInDir
関数のディレクトリを変更しましたが、jsonParsing.printDirtyJSON
の現在の作業ディレクトリを出力すると、変更された(新しい)ディレクトリが与えられます。Pythonの現在のディレクトリパスがクラス内で変更されました
私はクラスが自己完結型だと考えました。変更する前に、古いディレクトリへのパスが必要です。 dirStuff
クラスは、jsonParsing
クラスの前に呼び出されます。ありがとう。
class dirStuff:
def __init__(self, dirName):
self.dirName = dirName
def doesDirExit(self):
isLuisModelDir = os.path.isdir(self.dirName)
if isLuisModelDir:
print("""directory exists - Calling the findFileInLuisDir function""")
else:
print("****directory does not exist. Exiting****")
sys.exit()
def findFileInDir(self):
print("found the directory containing the utterances")
os.chdir(self.dirName)
luisModelList=[]
for file in glob.glob("*.JSON"):
luisModelList.append(file)
return luisModelList
class jsonParsing:
def __init__(self, dirName, fileName):
self.dirName = dirName
self.fileName = fileName
def printDirtyJSON(self):
luis_model_json = json.loads(open(self.fileName[1], encoding="utf8").read())
#open output file
try:
utterances_output = open('utterances.txt', "w", encoding="utf8")
# redirect utterances to the output file
print(os.getcwd())
for i in range(len(luis_model_json['utterances'])):
utterances = luis_model_json['utterances'][i]['text']
#print(utterances)
utterances_output.write(utterances+"\n")
#close the file
utterances_output.close()
except IOError:
print(""" Could not open the utterances file to write to""")
'のインスタンス – Saksow