• notice
  • Congratulations on the launch of the Sought Tech site

After pyinstaller is packaged, the configuration file cannot be read normally

The pyinstaller packaging configuration file cannot be read normally

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

Get the absolute path first before reading

pyinstaller stepped on another pit, configparser os.mknod

When using pyinstaller, use the configparser module.

Use relative paths. Tested in pycharm, normal, packaged into exe, there is an error

use an absolute path instead,

print ( os.getcwd ( ) )
fp_dir = os.getcwd ( ) _
print ( fp_dir )
fp = fp_dir + '\conf.ini' # define the configuration file name   
print ( fp )

Basically normal.

but met

conf . read ( fp ) # open conf  
    conf . add_section ( 'conf' ) # add conf node

File cannot be created automatically

Try os.mknod, it is not supported at all under windows.

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

Using the open method, the debugging was finally successful.

full code

def make_conf ():
    print ( 'make' )
    conf = ConfigParser () # instantiate   
    print ( 'No configuration file, creating' )
    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 ) # open conf  
    if type != 'up' :
        conf . add_section ( 'conf' ) # add conf node  
    print ( 'add section' )
    conf .set ( ' conf' , 'firefox' , firefox ) # add values   
    conf . set ( 'conf' , 'geckodriver' , geckodriver ) # add values   
    conf .set ( 'conf' , ' wechat ' , WeChat ) # add value    
    # conf.set('conf', 'firefox', '') # add values
    # conf.set('conf', 'geckodriver', '') # add value
    # conf.set('conf', 'wechat', '') # add value
    print ( 'set all' , fp )
    with open ( fp , 'w' ) as fw : # loop write    
        conf.write ( fw ) _ _
    return True

The above is a personal experience, I hope it can give you a reference, and I hope you will support us a lot.

Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

buy lipitor 20mg & lt;a href="https://lipiws.top/"& gt;buy lipitor 80mg generic& lt;/a& gt; lipitor over the counter

Ahobrv

2024-03-07

Leave a Reply

+