0

我有一些传感器供应商提供给我的 .exe 应用程序。它们允许我在特定时间抓取数据并转换文件类型。但是,我需要手动通过 cmd 运行它们,并且我正在尝试使用 Python 自动化该过程。我无法让这个工作。

到目前为止,我有:

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)

但是,“arcfetch” exe 没有运行。此外,这需要我允许 Python 每次都对硬盘进行更改,这不会自动工作。

任何帮助将不胜感激!

4

1 回答 1

0

经过一些玩耍和评论的帮助,我能够让它工作!

最终代码:

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)

编辑:这仍然存在管理问题,但我可以为此更改计算机上的安全设置。

于 2020-10-17T00:54:12.380 回答