Shutil 将文件保存为文件类型而不是 .txt

发布于 2025-01-12 15:49:59 字数 322 浏览 3 评论 0原文

我使用以下代码使用 Shutil 将几个 .txt 文件加入到一个文件中。保存的文件保存为“文件”格式,但我可以用记事本打开它。如何直接将文件保存为.txt?

    #Join generated text files to one file
    import shutil
    with open(img_file,'wb') as wfd:
        for f in out_file_list:
            with open(f,'rb') as fd:
                shutil.copyfileobj(fd, wfd)

I am using the following code to join a few .txt files to one file using Shutil. The saved file is saved as a 'File' format, but I can open it with Notepad. How can I save the file to .txt directly?

    #Join generated text files to one file
    import shutil
    with open(img_file,'wb') as wfd:
        for f in out_file_list:
            with open(f,'rb') as fd:
                shutil.copyfileobj(fd, wfd)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-01-19 15:49:59

@FoundAbetterName 的解决方案按预期工作。谢谢你!

    #Join generated text files to one file
    import shutil
    with open(img_file + ".txt",'wb') as wfd:
        for f in out_file_list:
            with open(f,'rb') as fd:
                shutil.copyfileobj(fd, wfd)

The solution from @FoundAbetterName works as intended. Thank you!

    #Join generated text files to one file
    import shutil
    with open(img_file + ".txt",'wb') as wfd:
        for f in out_file_list:
            with open(f,'rb') as fd:
                shutil.copyfileobj(fd, wfd)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文