pyinstaller,找不到文件回溯错误

时间:2019-02-24 20:04:39

标签: python pyinstaller

我用以下内容修改了.spec文件

`a.datas += [
    ('WORDS', '\words.txt', 'DATA'),
    ('CITIES', '\cities.txt', 'DATA'),
    ('ANIMALS', '/canimals.txt', 'DATA'),
    ]`
a = Analysis(...)之后的

。当我在规格文件上运行pyinstaller时,出现以下回溯错误

`Cannot find ('ANIMALS', '/canimals.txt', 1, 'x')
Traceback (most recent call last):
  File "c:\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python37\Scripts\pyinstaller.exe\__main__.py", line 9, in <module>
  File "c:\python37\lib\site-packages\PyInstaller\__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\python37\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\python37\lib\site-packages\PyInstaller\building\build_main.py", line 844, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\python37\lib\site-packages\PyInstaller\building\build_main.py", line 791, in build
    exec(code, spec_namespace)
  File "hangman.spec", line 37, in <module>
    console=True )
  File "c:\python37\lib\site-packages\PyInstaller\building\api.py", line 424, in __init__
    strip_binaries=self.strip, upx_binaries=self.upx,
  File "c:\python37\lib\site-packages\PyInstaller\building\api.py", line 196, in __init__
    self.__postinit__()
  File "c:\python37\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
    self.assemble()
  File "c:\python37\lib\site-packages\PyInstaller\building\api.py", line 281, in assemble
    pylib_name=pylib_name)
  File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 334, in __init__
    super(CArchiveWriter, self).__init__(archive_path, logical_toc)
  File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 61, in __init__
    self._add_from_table_of_contents(logical_toc)
  File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 86, in _add_from_table_of_contents
    self.add(toc_entry)  # The guts of the archive.
  File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 381, in add
    fh = open(pathnm, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/canimals.txt'`

a.datas中运行其他文件时,它不会产生任何错误,我已经验证了“ canimals.txt”文件及其存在,并删除并重新创建了该文件,并且仍然我同样的错误,我不明白发生了什么事,因为该文件显然存在。即时通讯是pyinstaller的新功能,我遵循了上一个问题的建议,而现在ive似乎陷入僵局。我该怎么办?

编辑:这是查询程序以打开指定文件的代码

if decision == '1':
    b = WordBank('words.txt')
    bank = b.returnBank()
elif decision == '2':
    b = WordBank('cities.txt')
    bank = b.returnBank()
elif decision == '3':
    b = WordBank('canimals.txt')
    bank = b.returnBank()

WordBank类如下

class WordBank:
    def __init__(self, file):
        file = open(file, 'r')
        self.word_bank = file.readlines()
        self.word_bank = ''.join(self.word_bank)
        self.word_bank = self.word_bank.split('\n')

我收到错误

Traceback (most recent call last):
    file "hangman.py", line 212. in <module>
    file "wordbank.py", line 3, in __init__
FileNotFoundError: [Errno 2] No such file or directory 'cities.txt'
[11452] failed to execute script hangman

我是否必须使用os模块指定文件路径?

1 个答案:

答案 0 :(得分:0)

从错误消息中可以明显看出您的问题:

FileNotFoundError: [Errno 2] No such file or directory: '/canimals.txt'`

您说文件“ canimals.txt”存在。但是您的代码不是在寻找名为“ canimals.txt”的文件。它正在寻找一个名为“ /canimals.txt”的文件。斜线是您的问题。我猜您在Windows上,正斜杠只是文件名或目录名中的任意字符。

您说其他两个文件有效,并且在它们的名称前面有反斜杠,而不是正斜杠,这一事实使问题更加明确。将正斜杠变成反斜杠,我敢打赌,您的代码将按您的意愿工作。

此外,您应该能够从文件名前面删除反斜杠,并使代码仍然有效。以这种方式使用的单个反斜杠试图“转义”其后的字符。但是转义常规字母只会给您字母本身,因此在这种情况下,反斜杠没有任何作用。