【问题标题】:Python launch exe file errorPython启动exe文件错误
【发布时间】:2015-01-14 17:46:01
【问题描述】:

我已经用 C# 编写了一个 .exe 文件,如果我手动启动它(双击)它可以正常工作,但每次我尝试用 python 打开它。

import  os
os.system('"D:\\XX\MyFile.exe"')

我也试过这个:

import subprocess 

exefile = 'D:\\XX\\MyFile.exe'
subprocess.call([exefile])

程序立即崩溃,提示“MyFile.exe 已停止工作。某个程序导致程序停止正常工作。请关闭该程序。”。

现在让我感到困惑的是,如果我手动运行它,但当我尝试通过 Python 启动它时它会失败。

C# 应用程序只是一个更新程序,我编写它来检查我的 pythonscripts 的更新。它有一个 .dll,其中包含更新例程和加载 (.dat) 文件以获取信息的表单。

有什么我想念的吗?

编辑:

经过一些修改和诊断,我的 exefile 现在启动了,但我意识到如果它是由 Python 启动的,exe 无法在我的 C# 应用程序中执行以下功能。

private UpdateSaveFile DecodeSaveFile(string LocalUpdateFile)
    {
        FileStream localFileStream = null;
        BinaryFormatter decoder = null;
        try
        {
            localFileStream = File.Open(LocalUpdateFile, FileMode.Open, FileAccess.Read);
            decoder = new BinaryFormatter();
            return (UpdateSaveFile)decoder.Deserialize(localFileStream);
        }
        catch (Exception e)
        {
            throw new InvalidDataException("The local update info file is corrupt!", e);
        }
        finally
        {
            if (localFileStream != null)
                localFileStream.Dispose();
        }

    }

现在,每当我从 python 运行 exefile 时,它​​都会向我抛出“本地更新信息文件已损坏”。但是,如果我手动运行 exefile(不使用 python)/cmd 提示符,它工作得非常好。

有人知道吗?是因为管理员权限吗? python启动的exe有管理员权限吗?

【问题讨论】:

  • 有什么理由你用exefile = '%s' % 'D:\\XX\\MyFile.exe' 而不是exefile = 'D:\\XX\\MyFile.exe' 吗?
  • 在第一个代码块中有四个引号对我来说似乎很奇怪。你试过os.system("D:\\XX\MyFile.exe")吗?
  • 我觉得os.system(r'D:\XX\MyFile.exe')更好
  • 为什么不只是 import subprocess subprocess.Popen([r"MyFile.exe"] ?你能试试这个吗
  • 我尝试了所有上述 cmets,但都是一样的 :( 我是否必须为 exe 指定任何内容?因为我的程序有一个 .dll 并且它可能不会运行?

标签: c# python


【解决方案1】:

好的,我找到了罪魁祸首。所以我有一个这样的字符串:它应该查看文件“Updates.dat”的当前目录。但是由于某些原因,如果我通过 Python 启动 exe。它无法识别“.\”,但如果我手动运行它,那很好。这真的让我很困惑。

const String LocalUpdateFile = @".\Updates.dat";

所以,如果我改成这个。 python 启动的 exe 运行良好。

const String LocalUpdateFile = @"D:\XX\Updates.dat";

但我真的很好奇为什么会发生这种情况。如果有人有任何解决方案,请告诉我。

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2018-12-06
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多