日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pyinstaller打包后,配置文件無法正常讀取的解決_python

作者:被污染的一張白紙 ? 更新時(shí)間: 2022-04-18 編程語言

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時(shí),有使用configparser模塊。

使用相對路徑。在pycharm中測試,正常,打包成exe,就出錯(cuò)了

換用絕對路徑,

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節(jié)點(diǎn)

不能自動(dòng)創(chuàng)建文件

嘗試os.mknod,windows下根本不支持。

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

用open方法,終于調(diào)試成功。

完整代碼

def make_conf():
? ? print('make')
? ? conf = ConfigParser() ?# 實(shí)例化
? ? print('沒有配置文件,創(chuàng)建中')
? ? 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節(jié)點(diǎn)
? ? 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: ?# 循環(huán)寫入
? ? ? ? conf.write(fw)
? ? return True

原文鏈接:https://blog.csdn.net/abzdasfad/article/details/106942892

欄目分類
最近更新