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-2025 CODEPRJ.COM