-1

I wanna export all information in Windows Task Scheduler using Python, so I wrote the code as follows:

Import subprocess

subprocess.call(r'schtasks.exe / query / FO CSV > D:\tasks.csv /V')

Then I got ERROR: Invalid argument/option - '>'

But when I input schtasks.exe / query / FO CSV > D:\tasks.csv /V in cmd, it works.

Can anybody help me with this? Thank you!

1 Answers1

0

In cmd it works because it's cmd interpreting the >. You aren't calling cmd here though, you are calling schtasks!

You can solve it in one of the following ways:

  • Use shell=True so that Python spawns a shell (presumably cmd, but that's actually determined by the COMSPEC environment variable) and runs the command there
  • Run cmd explicitly: cmd.exe /c "schtasks.exe /query /FO CSV > D:\tasks.csv /V"
  • Instead of using >, capture the output in Python and then write it into a file.
CherryDT
  • 25,571
  • 5
  • 49
  • 74