开发者

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 '
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜