开发者

Executing commands from windows batch file using Python, without creating batch file

开发者 https://www.devze.com 2023-03-15 14:12 出处:网络
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 \'

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

精彩评论

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