簡體   English   中英

將錯誤編譯消息保存在python3中的txt文件中

[英]save error compile message in txt file in python3

您能幫我編譯程序時,如果發生錯誤,如何在編譯終止之前重定向此錯誤消息到文本文件中?

我寫了這段代碼,但是我的問題是我想要在我的COMP_ERR.txt文件創建並在此文件內寫入錯誤之后出錯。 但在我的代碼中,此創建之前

import urllib.request
import os
import tempfile
import sys
import fileinput


original_stderr = sys.stderr
f_error = open("COMP_ERR.txt", "w")
sys.stderr = f_error
try:
    url = sys.argv[1]
    path_snip_file = sys.argv[2]

    #url = 'http://pages.di.unipi.it/corradini/Didattica/AP-17/PROG-ASS/03/ClassWithTest.java'
    #path_snip_file = "C:/Users/user/AppData/Local/Programs/Python/Python36/snip1.java"

    path_remote_file_inOurComp = "C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest.java"
    path_remote_file_inOurCom = "C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest1.java"
    gt_url = urllib.request.urlretrieve(url)

    print("the URL is: ") 
    print(gt_url)
    link = input("input the path of file: ")

    """
    f = open(link, "r")
    for line in f:
        print(line, end='')
    f.close()
    """

    #--------------------]
    #copy snipper java file inside remote file
    #[--------------------


    with open(path_remote_file_inOurComp, 'w') as modify_file:
        with open (link, 'r') as r_file:
            for line in r_file:
                if " public static void main(" not in line:
                    modify_file.write(line)
                else:
                    with open(path_snip_file, 'r') as snip_file:
                        for lines in snip_file:
                            modify_file.write(lines)
                    modify_file.write('\n'+line)
    #-------------------------------------------------------------
    #refactor
    #-------------------------------------------------------------
    with open(path_remote_file_inOurCom, 'w') as ft:
        with open(path_remote_file_inOurComp, 'r') as file_n:
            for line in file_n:
                line = line.replace("System.out.println(", "System.out.println(insertLength(")
                line = line.replace(";", ");")
                ft.write(line)


except IndexError:
    print("Not enough input! ! !")
sys.stderr = original_stderr
f_error.close()
logf = open("download.log", "w")
for download in download_list:
    try:
        # code to process download here
    except Exception as e:     # most generic exception you can catch
        logf.write("Failed to download {0}: {1}\n".format(str(download), str(e)))
        # optional: delete local version of failed download
    finally:
        # optional clean up code
        pass

除了這種方法之外,您還可以使用日志記錄庫來保存應用程序的每個日志。

以下是保存日志問題的方法2。

import logging
logging.basicConfig(filename='gunicon-server.log',level=logging.DEBUG,
                    format='[%(levelname)s]: [%(asctime)s] [%(message)s]', datefmt='%m/%d/%Y %I:%M:%S %p')
try:
    # code to process download here
except Exception as e:     # most generic exception you can catch
    logging.error(str(e))
finally:
    # optional clean up code
    pass

檢查它是否對您有用

import sys
try:
   raise
except Exception as err:
      **exc_type, exc_obj, exc_tb = sys.exc_info()**  # this is to get error line number and description.
      file_name = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]  # to get File Name.
      error_string="ERROR : Error Msg:{},File Name : {}, Line no : {}\n".format(err,file_name,exc_tb.tb_lineno))
      file_log = open("error_log.log", "a")
      file_log.write(error_string)
      file_log.close()
import sys
sys.stderr = open('errorlog.txt', 'w')

# do whatever

sys.stderr.close()
sys.stderr = sys.__stderr__

看到更多細節: https : //ubuntuforums.org/showthread.php?t=849752

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM