鱼C论坛

 找回密码
 立即注册
查看: 1946|回复: 1

python写入txt空行问题,哪位老大帮我解决一下可以

[复制链接]
发表于 2021-9-17 21:40:14 | 显示全部楼层 |阅读模式

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

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

x
可以看一下这个帖子
https://fishc.com.cn/thread-202362-1-1.html

资源在10楼
源码在14楼

哪位帮我测试一下,写回去之后出现空行,谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-17 22:03:17 | 显示全部楼层
  1. from tkinter.filedialog import askdirectory
  2. import tkinter.messagebox
  3. from tkinter import *
  4. import os
  5. import chardet


  6. def selectfile():
  7.     dir1 = askdirectory()
  8.     pattern = re.compile(r'{.*?}')
  9.     for file in os.listdir(dir1):
  10.         if os.path.isfile(os.path.join(dir1, file)):
  11.             filepath = os.path.join(dir1, file)
  12.             with open(filepath, 'rb') as f:
  13.                 rb = f.read()
  14.             code = chardet.detect(rb)['encoding']
  15.             with open(filepath, encoding=code) as f:
  16.                 m = pattern.sub('', f.read())
  17.             with open(filepath, "w") as f:
  18.                 f.write(m)
  19.     tkinter.messagebox.showinfo('提示', '操作完成')


  20. root = Tk()
  21. root.title('删除大括号')
  22. root.resizable(0, 0)
  23. root.geometry('250x150')
  24. Button(root, text="路径选择", command=selectfile).pack()

  25. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 11:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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