2017-01-20 60 views
1

我改變了dirStuff.findFileInDir函數中的目錄,但是當我打印出當前工作目錄jsonParsing.printDirtyJSON時,它給了我改變的(新)目錄。python中的當前目錄路徑在類中改變

我以爲班級是獨立的。在更改之前,我需要舊目錄的路徑。在jsonParsing類之前調用​​dirStuff類。謝謝。

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

'os.chdir'改變它的所有類的實例 – Saksow

回答

1

os.chdir更改整個過程的當前目錄。如果你能避免它,那就去做吧。

在這種情況下,你可以。替換此代碼:

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

如果你想保持相同的代碼和邏輯,你可以做這些編輯:

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 

隨着dirStuffObjdirStuff

相關問題