Python - 代码冻结后控制台关闭

Python - Console closes after the code has been frozen

我在 Python 2.7 中编写了一个应用程序,它使用 moviepy 检索媒体文件的长度。如果我从命令行 运行 它,一切正常;但是在冻结代码后,当我 运行 它时,控制台 window 立即关闭。我试过 cx_freeze、pyinstaller 和 py2exe,结果都一样。我的代码有问题吗?或者这是 moviepy 的问题?我在 Windows 10 上测试,这最终将在 Windows 7 上使用。这是代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Video-Audio Length Retriever
#
# Version: 0719A
#
# Author: Simon Lachaîne


import codecs
from moviepy.editor import VideoFileClip, AudioFileClip
import os


directories = []


def read_directories():
    global directories
    directories_txt = raw_input("Enter the path and name of the text file containing the source directories: ")

    with codecs.open(directories_txt, "r", encoding="utf8") as source_dirs:
        directories = [line.rstrip() for line in source_dirs]


def write_text(report, text2save):
    with open(report, "a") as report:
        report.write(text2save)


def check_duration():
    for directory in directories:
        for root, dirs, files in os.walk(directory):
            os.chdir(root)
            for fichier in files:

                try:
                    video = VideoFileClip(fichier)

                    m, s = divmod(video.duration, 60)
                    h, m = divmod(m, 60)

                    length = fichier + " ; " + "%02d:%02d:%02d\n" % (h, m, s)
                    write_text(durations_report, length)
                    print "Processed file " + fichier

                except IOError:
                    pass

                except KeyError:
                    try:
                        audio = AudioFileClip(fichier)

                        m, s = divmod(audio.duration, 60)
                        h, m = divmod(m, 60)

                        length = fichier + " ; " + "%02d:%02d:%02d\n" % (h, m, s)
                        write_text(durations_report, length)
                        print "Processed file " + fichier

                    except IOError:
                        pass


read_directories()
durations_report = raw_input("Enter the path and name of the report to create: ")
check_duration()

您可以从命令行运行 冻结的代码来查看错误消息。

就 pyinstaller 而言,我在 moviepy 的 hooks 文件夹中看不到挂钩,很可能它没有捆绑在冻结版本中。您可以将它(或任何其他可能丢失的东西)添加为隐藏导入:https://pythonhosted.org/PyInstaller/when-things-go-wrong.html?highlight=hidden#listing-hidden-imports