網(wǎng)站首頁 編程語言 正文
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
相關(guān)推薦
- 2022-09-02 基于Python實(shí)現(xiàn)配置熱加載的方法詳解_python
- 2022-06-25 Python+PuLP實(shí)現(xiàn)線性規(guī)劃的求解_python
- 2022-12-21 React?之最小堆min?heap圖文詳解_React
- 2022-12-12 BAT腳本接收輸入數(shù)字_DOS/BAT
- 2022-10-13 Python詳細(xì)介紹模型封裝部署流程_python
- 2022-02-24 Promise.all() 函數(shù)的常用場景
- 2023-03-27 Android三種雙屏異顯實(shí)現(xiàn)方法介紹_Android
- 2022-10-11 詳解pandas?df.iloc[]的典型用法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支