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!

4
  • CD is an internal command within cmd.exe. Could you please explain what you're intending to do here "cd", "cd C:/reftek/bin",? Commented Oct 16, 2020 at 21:08
  • I am trying to get to the directory where the .exe file is located. Commented Oct 16, 2020 at 21:30
  • 1
    Does this answer assist you? Subprocess changing directory Commented Oct 16, 2020 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. Commented Oct 17, 2020 at 0:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.