網站首頁 編程語言 正文
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
原文鏈接:https://blog.csdn.net/abzdasfad/article/details/106942892
相關推薦
- 2022-11-14 基于統計自適應線性回歸-目標尺寸預測
- 2022-11-26 Python反向傳播實現線性回歸步驟詳細講解_python
- 2021-12-15 go語言處理TCP拆包/粘包的具體實現_Golang
- 2022-07-01 Keras實現Vision?Transformer?VIT模型示例詳解_python
- 2022-06-13 C語言strlen函數實現讀取字符串長度詳解_C 語言
- 2022-05-21 ASP.NET?MVC中_ViewStart.cshtml作用介紹_基礎應用
- 2023-01-17 python實現鄰接表轉鄰接矩陣_python
- 2022-08-19 Python運行時修改業務SQL代碼_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支