iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pyinstaller打包后,配置文件无法正常读取的解决
  • 436
分享到

pyinstaller打包后,配置文件无法正常读取的解决

2024-04-02 19:04:59 436人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录pyinstaller打包配置文件无法正常读取pyinstaller又踩一坑,configparser os.mknod完整代码pyinstaller打包配置文件无法正常读取 i

pyinstaller打包配置文件无法正常读取

import os
file = os.path.dirname(os.path.abspath(__file__))
cf = configparser.ConfigParser()
print(file)
cf.read(file+'/data.ini')

先获取绝对路径在读取

pyinstaller又踩一坑,configparser os.mknod

在使用pyinstaller时,有使用configparser模块。

使用相对路径。在PyCharm测试,正常,打包成exe,就出错了

换用绝对路径,

print(os.getcwd())
fp_dir=os.getcwd()
print(fp_dir)
fp = fp_dir + '\conf.ini'  # 定义配置文件名
print(fp)

基本正常。

可是遇到了

conf.read(fp)  # 打开conf
    conf.add_section('conf')  # 添加conf节点

不能自动创建文件

尝试os.mknod,windows下根本不支持。

    tes = open(fp,'a')
    tes.close()

用open方法,终于调试成功。

完整代码

def make_conf():
    print('make')
    conf = ConfigParser()  # 实例化
    print('没有配置文件,创建中')
    tes = open(fp, 'a')
    tes.close()
    firefox = str(get_extension(['firefox.exe']))
    geckodriver = str(get_extension(['geckodriver.exe']))
    WeChat = str(get_extension(['WeChat.exe']))
    conf.read(fp)  # 打开conf
    if type!='up':
        conf.add_section('conf')  # 添加conf节点
    print('add section')
    conf.set('conf', 'firefox', firefox)  # 添加值
    conf.set('conf', 'geckodriver', geckodriver)  # 添加值
    conf.set('conf', 'wechat', WeChat)  # 添加值
    # conf.set('conf', 'firefox', '')  # 添加值
    # conf.set('conf', 'geckodriver', '')  # 添加值
    # conf.set('conf', 'wechat', '')  # 添加值
    print('set all', fp)
    with open(fp, 'w') as fw:  # 循环写入
        conf.write(fw)
    return True

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: pyinstaller打包后,配置文件无法正常读取的解决

本文链接: https://www.lsjlt.com/news/138903.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作