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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM