1

I have some .exe applications that were given to me by a supplier of sensors. They allow me to grab data at specific times and convert file types. But, I need to run them through the cmd manually, and I am trying to automate the process with Python. I am having trouble getting this to work.

So far, I have:

import sys
import ctypes
import subprocess

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False
if is_admin():
    process = subprocess.Popen('arcfetch C:/reftek/arc_pas *,2,*,20:280:12:00:000,+180', shell=True, cwd="C:/reftek/bin",
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
    out = process.stdout.read()
    err = process.stderr.read()

else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

But, the "arcfetch" exe was not ran. Also, this requires me to allow Python to make changes to the hard drive each time, which won't work automatically.

Any assistance would be greatly appreciated!

Ken White
  • 123,280
  • 14
  • 225
  • 444
Esther
  • 21
  • 3
  • `CD` is an internal command within `cmd.exe`. Could you please explain what you're intending to do here `"cd", "cd C:/reftek/bin",`? – Compo Oct 16 '20 at 21:08
  • I am trying to get to the directory where the .exe file is located. – Esther Oct 16 '20 at 21:30
  • 1
    Does this answer assist you? [Subprocess changing directory](https://stackoverflow.com/questions/21406887/subprocess-changing-directory) – Compo Oct 16 '20 at 22:34
  • Thanks! I did change the code, but unfortunately it still doesn't work. I edited the question to reflect the new code. – Esther Oct 17 '20 at 00:32

1 Answers1

1

After some playing around and assistance from comments, I was able to get it to work!

The final code:

import sys
import ctypes
import subprocess

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False
if is_admin():
    subprocess.run('arcfetch C:/reftek/arc_pas *,2,*,20:280:12:00:000,+180', shell=True, check=True, cwd="C:/reftek/bin")

else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

Edit: This still has the admin issue, but I can change the security settings on my computer for that.

Esther
  • 21
  • 3