Executing commands from windows batch file using Python, without creating batch file
I need to execute below command (and couple of other similar) to collect Windows OS event logs:
' wmic nteventlog where filename="appevent" call BackupEventLog C:\appevent.evt '
The command executes successfully through cmd prompt. and collects file C:\appevent.evt
But when i use Python os.system
or os.popen
to execute it retruns error.
Also if I create a .bat
file with the above command and execute the .bat
using os.syst开发者_Python百科em
it works
properly,
What is going wrong when I execute cmd using os.system
?
and How can I execute the command using Python?
Its due to the \a
in the string. Escape the \
in the string by replacing it with \\
:
' wmic nteventlog where filename="appevent" call BackupEventLog C:\\appevent.evt '
or use a raw string:
r' wmic nteventlog where filename="appevent" call BackupEventLog C:\appevent.evt '
精彩评论