用python将多个txt文件合并

 (下次想看什么,请在评论区留言!)

这里用到了wxpython界面

装wx库,可pycharm在终端输入以下命令(清华大学镜像)

pip install -U wxPython -i https://pypi.tuna.tsinghua.edu.cn/simple

下面是源代码

import wx
import os

class MergeFilesFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title="Merge Text Files", size=(400, 200))
        panel = wx.Panel(self)

        self.file_picker = wx.Button(panel, label="Select Text Files", size=(200, -1))
        self.merge_button = wx.Button(panel, label="Merge", size=(100, -1))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(wx.StaticText(panel, label="Select Text Files to Merge:"), 0, wx.ALL, 10)
        sizer.Add(self.file_picker, 0, wx.EXPAND|wx.ALL, 10)
        sizer.Add(self.merge_button, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 10)

        panel.SetSizer(sizer)

        self.file_picker.Bind(wx.EVT_BUTTON, self.on_select_files)
        self.merge_button.Bind(wx.EVT_BUTTON, self.on_merge)

    def on_select_files(self, event):
        with wx.FileDialog(self, "Select Text Files to Merge", wildcard="Text files (*.txt)|*.txt",
                           style=wx.FD_OPEN | wx.FD_MULTIPLE) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            self.selected_files = fileDialog.GetPaths()
            if not self.selected_files:
                wx.MessageBox("Please select at least one file to merge!", "Error", wx.OK | wx.ICON_ERROR)
                return

            # Display selected files
            selected_files_text = '\n'.join(self.selected_files)
            wx.MessageBox(f"Selected Files:\n{selected_files_text}", "Selected Files", wx.OK | wx.ICON_INFORMATION)

    def on_merge(self, event):
        try:
            selected_files = self.selected_files
        except AttributeError:
            wx.MessageBox("Please select files first!", "Error", wx.OK | wx.ICON_ERROR)
            return

        with wx.DirDialog(self, "Select Target Folder for Merged File", style=wx.DD_DEFAULT_STYLE) as dirDialog:
            if dirDialog.ShowModal() == wx.ID_CANCEL:
                return

            target_folder = dirDialog.GetPath()
            merged_file_path = os.path.join(target_folder, "merged_output.txt")

            with open(merged_file_path, 'w', encoding='utf-8') as output:
                for file_path in selected_files:
                    with open(file_path, 'r', encoding='utf-8') as file:
                        for line in file:
                            output.write(line)

            wx.MessageBox(f"Files merged successfully!\nMerged file saved at: {merged_file_path}", "Success", wx.OK | wx.ICON_INFORMATION)

if __name__ == "__main__":
    app = wx.App(False)
    frame = MergeFilesFrame()
    frame.Show()
    app.MainLoop()

运行结果如下:

此外,还可以用pyinstaller来打包为exe可执行文件,生成的exe在dist目录里。

pyinstaller --onefile --noconsole 文件合并(你的文件名).py

这样你就获得了一个txt文件合并软件!!

感谢观看!!!

  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值