2017-01-20 3 views
1

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""") 
+0

'のインスタンス – Saksow

答えて

1

os.chdirは、プロセス全体の現在のディレクトリを変更します。あなたがそれを避けることができれば、それをしてください。

その場合は可能です。リストの内包表記を使用して、

def findFileInDir(self): 
    print("found the directory containing the utterances") 
    luisModelList=[] 
    for file in glob.glob(os.path.join(self.dirName,"*.JSON")): 
     luisModelList.append(os.path.basename(file)) 
    return luisModelList 

以上のコンパクト::まったく同じことが、ディレクトリを変更する必要なしにしても、そのコードによる

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 

:このコードを置き換え

def findFileInDir(self): 
    print("found the directory containing the utterances") 
    return [os.path.basename(file) for file in glob.glob(os.path.join(self.dirName,"*.JSON"))] 

別の方法では、os.listdir()fnmatchモジュール(os.listdirは現在のディレクトリを出力に追加しません):

def findFileInDir(self): 
    print("found the directory containing the utterances") 
    return [file for file in os.listdir(self.dirName) if fnmatch.fnmatch(file,"*.JSON")] 

一般的な選択肢は、古いパスを保存chdirを実行し、以前のパスを復元するには、次のようになります。

previous_path = os.getcwd() 
os.chdir(new_path) 
... 
os.chdir(previous_path) 

いますが、例外を処理しなければならない(finallyブロックでパスリストアを置きます)エラーが発生した場合、パスが間違った値に設定されないようにします。私はそれをお勧めしません。

0

あなたは同じコードとロジックを保持したい場合は、あなたがこれらの編集を行うことができます。dirStuffObj

class dirStuff: 
    def __init__(self, dirName): 
     self.dirName = dirName 
     self.currentDirName = os.getcwd() 

..... 


class jsonParsing: 
    def __init__(self, dirName, fileName, dirStuffObj): 
     self.dirName = dirName 
     self.fileName = fileName 
     self.currentDirName = dirStuffObj.currentDirName 

すべてのクラスのためにそれを変更しos.chdir` dirStuff

関連する問題