Quantcast
Channel: Active questions tagged windows - Super User
Viewing all articles
Browse latest Browse all 10777

How open a .log file in Notepad with Python without the file continuing to be linked to Python execution? [migrated]

$
0
0

When I open the Windows file explorer and double-click the test_errors.log file, it opens in Notepad and I can edit it by directly saving the edits with Control + S.

But I want to do this with Python, but as the code opens the file and wait for me to reply the input to continue execution, when I try to save the edits using Control + S it opens the window to save as a new file:

enter image description here

from pathlib import Pathimport loggingimport sysimport osCODE_NAME_FILE = os.path.splitext(os.path.basename(__file__))[0]ERROR_LOG_FILE = f'{CODE_NAME_FILE}_errors.log'def setup_logger(logger_name: str, log_file: str, level: int = logging.DEBUG) -> logging.Logger:    logger = logging.getLogger(logger_name)    logger.setLevel(level)    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')    file_handler = logging.FileHandler(log_file)    file_handler.setFormatter(formatter)    logger.addHandler(file_handler)    return loggerdef log_exception(comments: str | int | float = '') -> None:    _, _, tb = sys.exc_info()    if tb is not None:        logger.error(f"Line Error: {tb.tb_lineno}\nComments: {comments}", exc_info=True)    else:        logger.error(f"log_exception() called without an active exception.\nComments: {comments}")logger = setup_logger(__name__, ERROR_LOG_FILE)def main():    while True:        try:            # works        except Exception as e:            log_exception()            os.startfile(Path(ERROR_LOG_FILE).resolve())            input("Log is open to analyze the error")if __name__ == '__main__':    main()

If the code were like this, just opening the file and finishing execution, I can save it as I want because the Python code doesn't continue running:

from pathlib import Pathimport osERROR_LOG_FILE = "test_errors.log"os.startfile(Path(ERROR_LOG_FILE).resolve())

PS: using automated movements with the mouse (pyautogui) or keyboard (pynput.keyboard) to find the file and open it as done manually cannot be an option because I use the computer for other things while the file is not opened.

How can I open it with Python without the file being linked to the execution of the Python file?


Viewing all articles
Browse latest Browse all 10777

Trending Articles