Could you help me that when I compile my program and if I have an error how can I redirect this error message in text file, before compilation terminate?
I wrote this code, But my problem is i want when i have an error after that my COMP_ERR.txt file create and write error inside this file. but in my code this create before
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()
 
     
     
    