【问题标题】:Pyinstaller throwing error No such file or directoryPyinstaller 抛出错误没有这样的文件或目录
【发布时间】:2016-02-28 17:02:21
【问题描述】:

我编写了一个简单的 python 文件,我试图在其中读取文本文件并将一些日志写入输出文件。使用 pyinstaller 我创建了一个 .exe 文件。但是当我执行那个 .exe 文件时,它会抛出以下错误。

Traceback (most recent call last):
  File "<string>", line 31, in <module>
  File "<string>", line 15, in process_data
IOError: [Errno 2] No such file or directory: 'input1.txt'
mytest returned -1

这是我的代码。

import re
import sys
import mytest2

def process_data(name, course):

    tmp = sys.stdout
    sys.stdout = open("out11.txt", 'w')

    if re.search("^ank", name):
        print "Yes I am here"
    else:
        print "No no wrong door"

    fr = open("input1.txt", "r")
    lines = fr.readlines()
    fr.close()

    print "Printing from input file.."
    for line in lines:
        print line.strip()

    sys.stdout.close()
    sys.stdout = tmp


if __name__ == "__main__":
    arg1 = sys.argv[1]
    arg2 = sys.argv[2]

    process_data(arg1, arg2)

谁能告诉我如何解决这个问题。我正在 Windows 中执行此操作。

我还想知道这个可执行文件是否可以在所有 Windows 操作系统(如 win 8、8.1、10 等)中运行。

【问题讨论】:

    标签: python windows executable pyinstaller


    【解决方案1】:

    input1.txt 不在您拥有 .exe 的文件夹中,或者 .exe 期望您将 input1.txt 打包到其中 - pyinstaller 中的 onefile/singlefile 选项。

    【讨论】:

    • 它在同一个文件夹中,我在 pyinstaller 中使用了 --onefile 选项
    • 尝试将input1.txt 打包到您的.exe 中或将文件路径作为参数传递
    • 嘿,谢谢。它没有抛出那个错误。但我现在遇到了不同的错误。
    • SyntaxError: 第 1 行文件 test1.exe 中的非 ASCII 字符“\x90”,但未声明编码;详情见python.org/peps/pep-0263.html
    • 我已经添加了 # -- coding: utf-8 -- 并尝试了它仍然给出了上述错误。你有什么想法吗
    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多