在Python中重现Unix cat命令

在Python中重现Unix cat命令,第1张

在Python中重现Unix cat命令

最简单的方法可能就是忘掉行,只读取整个文件,然后将其写入输出:

with open('command.fort.13', 'wb') as outFile:    with open('command.info', 'rb') as com, open('fort.13', 'rb') as fort13:        outFile.write(com.read())        outFile.write(fort13.read())

正如评论中指出的那样,如果两个输入中的任何一个输入较大(因为它将首先将整个文件复制到内存中),这可能会导致大量的内存使用。如果这可能是一个问题,那么以下 *** 作也将同样有效(通过分块复制输入文件):

import shutilwith open('command.fort.13', 'wb') as outFile:    with open('command.info', 'rb') as com, open('fort.13', 'rb') as fort13:        shutil.copyfileobj(com, outFile)        shutil.copyfileobj(fort13, outFile)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5646242.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存