2013-03-12 28 views
2

的Python:文件处理程序问题:删除文件,而无需离开。NSF我有如下方法来处理我的Python程序日志文件

def createLogger(logger, logLang): 
    """ 
    Setting up logger 
    """ 
    log_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") 
    file_handler = logging.FileHandler(filename=(os.path.join(OUT_DIR_LOGS, logLang + '-userdynamics.log'))) 
    file_handler.setFormatter(log_format) 
    logger.setLevel(logging.INFO) 
    logger.addHandler(file_handler) 

这是一个大的数据集的代码库,避免配额远程服务器上的限制,我已经实现以下gzip和焦油的过程,

def gzipLogs(lang): 
    """ 
    Compressing and tar 
    """ 
    # Compressing logfiles and removing the old logfile 
    original_filePath = OUT_DIR_LOGS + "/" +lang + "-userdynamics.log" 
    gzip_filePath = OUT_DIR_LOGS + "/" + lang +"-userdynamics.gz" 
    with open(original_filePath , 'rb') as original_file: 
     with gzip.open(gzip_filePath, 'wb') as zipped_file: 
      zipped_file.writelines(original_file) 
    os.remove(original_filePath) 
    # Compressing language folders that has data 
    folder_path = OUT_DIR + "/" + lang 
    tar_file = tarfile.open(folder_path + ".tgz", "w:gz") 
    # add timestamp to arch file 
    tar_file.add(folder_path, arcname = NOW + "_" + lang) 
    tar_file.close() 
    # delete the original file 
    shutil.rmtree(folder_path) 

我做我的数据在嵌套for循环收集过程,我称之为记录器为波纹管提到:

for something in somethings: 
    for item in items: 
     log = logging.getLogger() 
     # Calling the logging configuration function. 
     createLogger(log, lang) 

一切工作正常,但当它被执行时,在删除文件后,.nsf文件残留会导致配额问题保持原样。

所以我添加下面的代码段,关闭日志文件处理程序,但这个现在我endup收到以下错误:

代码以关闭日志文件:

unclosed_logs = list(log.handlers) 
for uFile in unclosed_logs: 
    print uFile 
    log.removeHandler(uFile) 
    uFile.flush() 
    uFile.close() 

上面的代码最终给我这个错误:

Traceback (most recent call last): 
    File "/somefilepath/SomePythonFile.py", line 529, in <module> 
    main() 
    File "/somefilepath/SomePythonFile.py", line 521, in main 
    gzipLogs(lang) 
    File "/somefilepath/SomePythonFile.py", line 61, in gzipLogs 
    with gzip.open(gzip_filePath, 'wb') as zipped_file: 
AttributeError: GzipFile instance has no attribute '__exit__' 

这是怎样的方法主要看起来像处理程序关闭代码段:

for something in somethings: 
    for item in items: 
     log = logging.getLogger() 
     # Calling the logging configuration function. 
     createLogger(log, lang) 
    unclosed_logs = list(log.handlers) 
    for uFile in unclosed_logs: 
     print uFile 
     log.removeHandler(uFile) 
     uFile.flush() 
     uFile.close() 

我在做什么错?我处理记录器是否错误?还是我过早关闭文件?

回答

0

经过一番研究,我意识到服务器,我正在执行该文件运行在Python 2.6和2.6 GZip模块没有with open。要回答这个问题,要么切换到python 2.7,要么在try catch块中将实现更改为旧时尚文件打开文件。

try: 
    inOriginalFile = open(original_filePath, 'rb') 
    outGZipFile = gzip.open(gzip_filePath, 'wb') 
    try: 
     outGZipFile.writelines(inOriginalFile) 
    finally: 
     outGZipFile.close() 
     inOriginalFile.close() 
except IOError as e: 
    logging.error("Unable to open gzip files, GZIP FAILURE") 

这是我如何解决这个问题。

1

有许多的事情,可能导致问题:

  1. 您应该只配置日志记录(例如设定水平,添加处理)在一个地方在你的程序,最好从if __name__ == '__main__'条款。你似乎没有这样做。请注意,您可以使用WatchedFileHandler并使用外部旋转器来旋转日志文件 - 例如logrotate提供旋转和压缩功能。
  2. __exit__有关的错误与日志无关 - 它可能与Python版本问题有关。 GZipFile只能在Python 2.7/3.2中与with一起使用 - 在旧版本中,如果您尝试在with语句中使用GZipFile,则会收到错误消息。
相关问题