1

I have a powershell script and I save the log using this way

$Log = Start-Transcript -Path $Log_Path -Force

How do I use it in Python?

2

2 Answers 2

4

You can write/save the log file using the below command.

logging.basicConfig(filename='path to the log file', level=...)

There is a filemode option to overwrite the file. To overwrite you can specifie filemode:w

logging.basicConfig(filename='logs.log',
            filemode='w',
            level=logging.INFO)

filemode: Specifies the mode to open the file, if filename is specified (if filemode is unspecified, it defaults to ‘a’).

Sign up to request clarification or add additional context in comments.

2 Comments

this way I can save the log to file, but its not overwrite the log, how to make it overwrite?
Just add filemode:w to overwrite the file.
1

There is a logging module in python. You can import an use that.

import logging logging.basicConfig(level=logging.DEBUG) logging.debug('This will get logged')

And the output will be:

DEBUG:root:This will get logged

Similarly, you can use the other log levels too. You can find more about it here

1 Comment

it does logged but it didnt write to file like the OP wants

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.