1

I am trying to run CPU-Z with Python to save a system report in a text file. This code is calling UAC in Win10, if cmd has no admin rights and then second cmd window opens:

import subprocess
import ctypes
import sys

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    subprocess.call(['cpuz_x32.exe', '-txt=report'])
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

When I'm in Python interpreter, 0 is returned. However, there is no report.txt file created. cpuz_x32.exe is in the same directory from where I started interpreter.

This was on Windows10. subprocess.call asked for elevated cmd, so this called UAC.

Then I decided to try this code in one of my WinXP virtual machines. No UAC there. Script runned well. No problems at all. The report.txt file was created. Is this bug in Win10 cmd, subprocess or Python 3.4.3?

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41
  • Have you tried calling it directly from the command line, to see whether it's a Python problem or a CPU-Z problem? – user234461 Mar 16 '18 at 12:11
  • Yes, I have. It works directly from cmd in Windows, like this `cpuz_x32.exe -txt=report` – Hrvoje T Mar 16 '18 at 12:12
  • If your code just ends here, the possibility is that python interpreter ends without waiting for process to finish, and hence you don't see output. Try this with `process = subprocess.Popen(["your_cmd"]...) process.wait()"` as described [here](https://stackoverflow.com/questions/28284715/python-subprocess-popen-wait-for-completion) – Aritesh Mar 16 '18 at 12:35
  • No change. After I enter p.wait() it returns 0 and no file is created. – Hrvoje T Mar 16 '18 at 13:14
  • @Aritesh "`subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)` Run the command described by args. Wait for command to complete, then return." – user234461 Mar 16 '18 at 14:50
  • Tried in WinXP and it runs ok. The report.txt file was created. So, the code is good. It has something to do with eleveted cmd window, when you run the script without admin rights. I click `yes` in UAC but subprocess.call obviously doesn't run and has not admin rights still. – Hrvoje T Mar 16 '18 at 23:07

0 Answers0