2017-02-23 91 views
1

我試圖導入一個名爲從名爲data_processed.py到同一目錄anothe文件名爲failed_hmac.py做一些百分比計算,但後一個Python文件log_entry_counts變量導入,我無法訪問導入模塊(文件)內的變量。這是第一個文件名data_processed.py。 進口OS 從日期時間日期時間進口,timedelta 從收藏導入櫃檯Python的導入文件模塊到另一個

def dataCount(folderName): 
    #count = 0 
    log_entry_counts = Counter() 
    today_date = datetime.today() 
    date_ranges = [ 
        ('30 Days', today_date - timedelta(days=30)), 
        # ('3 months', today-date - timedelta(days=90)), 
        #('year', today-date - timedelta(days=365)) 
        ] 
    for path, dirs, files in os.walk(folderName): 
     for dirname in dirs: 
      log_date = (os.path.join(path, dirname)) 

     for items in files: 
      if items != ".DS_Store": 
       try: 
        log_date = datetime.strptime(path[39:47], '%m%d%Y') 
        for text, dr in date_ranges: 
         if log_date >= dr: 
          log_entry_counts[text] += 1 
       except ValueError: 
        print 'This line has a problem:', log_date 

    total = 0 

    print log_entry_counts['30 Days'] 

def main(): 

    filePath = 'file.txt' 

    hmacCount(filePath) 

if __name__ == "__main__": 

    main() 

它通過循環的文件夾並計算所有子文件夾內的文件。另一個文件名failed_hmac.py如下

import os, sys 
from datetime import datetime, timedelta 
from collections import Counter 
import data_processed 

def hmacCount(fileName): 
    # Get the last failed hmac date 
    fileHandle = open('file.txt',"r") 
    lineList = fileHandle.readlines() 
    fileHandle.close() 
    lastLine = lineList[-1] 
    lastDate = datetime.strptime(lastLine[:10], '%m/%d/%Y') 

    with open(fileName) as f_input: 

     logEntryCounts = Counter() 

     #today_date = datetime.today() - timedelta(days=14) 
       #print today_date 
     dateRanges = [ 
        ('30 Days', lastDate - timedelta(days=30)), 
        #('3 months', lastDate - timedelta(days=90)), 
        #('One year', lastDate - timedelta(days=330)) 
        ] 

     for line in f_input: 

      #Stop Processing if there are no more lines 
      if not line: 

       break 

      if "Following hmac" in line: 

       try: 
        logDate = datetime.strptime(line[:10], '%m/%d/%Y') 

        for text, dr in dateRanges: 

         if logDate >= dr: 

          logEntryCounts[text] += 1 

       except ValueError: 

        print 'This line has a problem:', logDate 

    total = 0 

    hmacData = float(logEntryCounts['30 Days']) 

    print logEntryCounts['30 Days'] 

# Call The function 
def main(): 

    filePath = 'file.txt' 

    hmacCount(filePath) 

if __name__ == "__main__": 

    main() 

的目標是導入data_processed.pyfailed_hmac.py和使用變量logEntryCountslog_entry_counts執行一些百分比計算,但我一直得到logEntryCounts沒有定義錯誤

回答

0

data_entry_logs是可變insid e在data_processed.py中的功能。您將不得不修改您的代碼以調用函數data_processed.dataCount()並返回該值,以便您可以使用該變量。

data_processed.py

def dataCount(folderName): 
    #your code 
    return log_entry_counts 

failed_hmac.py

更換

hmacData = float(logEntryCounts['30 Days']) 

hmacData = float(data_processed.dataCount(...)['30 Days']) 

另一種方法,我認爲更簡潔的方法是將這兩個函數聲明爲一個類,如this question.

相關問題