Search code examples
pythonpython-3.xcommand-linesubprocesspython-os

Python returns error, but Command Line doesn't


I'm trying to use this command to get all running processes

wmic /output:C:\\ProcessList.txt process get caption

When I run this in cmd it works and outputs everything to the specified file.

When I try running this with Python I get an error saying:

Invalid file name.

I tried running it with os.system and subprocess.call, but both return an error.

os.system('wmic /output:C:\\ProcessList.txt process get caption')
subprocess.call('wmic /output:C:\\ProcessList.txt process get caption')

I also tried using raw strings.

os.system(r'wmic /output:C:\ProcessList.txt process get caption')
subprocess.call(r'wmic /output:C:\ProcessList.txt process get caption')

But nothing worked so far.


Solution

  • @Carcigenicate

    helped me reach the answer. The code from his answer gave me the real error. My code was giving me Invalid file name. His code returned: Permission denied: C\\ProcessList.txt.So it was a permission problem. I just ran my IDE with admin rights and it worked.

    To avoid having to run my IDE in administrator mode all the time, I just buried the file into Documents, so I don't need admin privileges to access it.