更新数据库在导出为EXE之后

2024-06-10 22:07:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在导出一个用Python编写的脚本,该脚本向投影仪的IP地址发送命令以关闭投影仪。代码的功能正常,投影仪将关闭。投影仪列表存储在与脚本不同的文件中的字典中,以允许其他脚本对其进行编辑和访问。你知道吗

一旦我使用Pyinstaller v3.3.1 for Python 3.5.1将脚本导出到exe,.exe文件不再从包含字典的.py文件更新,而是在内存中存储了一个版本,我无法更新。你知道吗

如何使可执行文件仍然从字典中读取并在每次运行时更新?你知道吗

谢谢你, 乔希

代码: dictonaryfile.py文件(为安全起见减少了,但显示了格式)。你知道吗

projectors = {
    '1': '192.168.0.1'
}

执行关机的脚本

from pypjlink import Projector
from file3 import projectors

for item in projectors:
    try:
        myProjector = Projector.from_address(projectors[item])
        myProjector.authenticate('PASSWORD REMOVED FOR SECURITY')
        state = myProjector.get_power()
        try:
            if state == 'on':
                myProjector.set_power('off')
                print('Successfully powered off: ', item)
        except:
            print('The projector in ', item, ', raised an error, shutdown 
may not have occurred')
            continue
    except:
        print(item, 'did not respond to state request, it is likely powered 
off at the wall.')
        continue

Tags: 文件代码frompy脚本for字典item
1条回答
网友
1楼 · 发布于 2024-06-10 22:07:12

正如您所注意到的,一旦创建了exe,就无法更新它。解决这类问题的方法是询问代码中dictonaryfile.py的位置-

from pypjlink import Projector

projector_location = input('Enter projector file location')
with open(projector_location) as f:
    for line in f:
        # extract data from f
        ....

对于这样的应用程序,最好使用配置文件(.ini),python有Configparser来读取配置文件。您可以将配置文件创建为-

[Projectors]
1 = '192.168.0.1'
2 = '192.168.0.45' # ..and so on

并用Configparser-

config = configparser.ConfigParser()
config.read(projector_location)
projectors = config['PROJECTORS']
for k, v in projectors.items():
    # your code here

相关问题 更多 >