鱼C论坛

 找回密码
 立即注册
查看: 2259|回复: 2

[已解决]python3.5 使用pyinstaller出现UnicodeDecodeError错误!!!!!!!!!!!

[复制链接]
发表于 2017-3-25 17:13:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
环境 win10 64位 python版本3.5.3 下面是源代码
  1. import os
  2. import easygui as g
  3. import sys

  4. def get_target():
  5.         target=[]
  6.         while 1:
  7.                 add = g.enterbox('请输入需要检索的格式(输入s停止输入):')
  8.                 if add == 's':
  9.                         break
  10.                 elif add == None:
  11.                         exit(0)
  12.                 else:
  13.                         target.append('.'+add)
  14.         return target


  15. def search_file(address,target):
  16.         os.chdir(address)
  17.                
  18.         for each_file in os.listdir(os.curdir):
  19.                 ext = os.path.splitext(each_file)[1]
  20.                 if ext in target:
  21.                         file_list.append(os.getcwd()+os.sep+each_file+os.linesep)
  22.                 if os.path.isdir(each_file):
  23.                         search_file(each_file,target)
  24.                         os.chdir(os.pardir)

  25. file_list=[]
  26.                        
  27. address=g.diropenbox('请输入需要搜索的路径:')
  28. target=get_target()
  29. search_file(address,target)
  30. save_list_path=g.diropenbox('请输入列表保存的路径:')

  31. f = open(save_list_path+os.sep+'file_list2.txt','w')
  32. f.writelines(file_list)
  33. f.close
复制代码
最佳答案
2017-3-26 00:52:05

test.txt 当做你要打开的文件,改成这样试试

  1.     try:
  2.         with open("test.txt","a") as f:
  3.             f.write(file_list+"\n")
  4.     except UnicodeEncodeError:
  5.         with open("test.txt","a") as f:
  6.             f.write(file_list.encode("gbk","ignore").decode("gbk")+"\n")
  7.     except Exception as e:
  8.         print(e)
复制代码

QQ截图20170325171132.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-3-25 17:15:34 | 显示全部楼层
百度了都说是编码问题可是具体怎么解决?需要修改我的代码还是怎么滴?
大神帮帮忙啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-26 00:52:05 | 显示全部楼层    本楼为最佳答案   

test.txt 当做你要打开的文件,改成这样试试

  1.     try:
  2.         with open("test.txt","a") as f:
  3.             f.write(file_list+"\n")
  4.     except UnicodeEncodeError:
  5.         with open("test.txt","a") as f:
  6.             f.write(file_list.encode("gbk","ignore").decode("gbk")+"\n")
  7.     except Exception as e:
  8.         print(e)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-26 09:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表