此代码打印所有 bash 历史记录,但我必须打印最后 20 个 bash 历史记录。我该怎么做?
import fpdf
pdf = fpdf.FPDF(format='letter')
pdf.add_page()
pdf.set_font("Arial", size=14)
for history in open('.bash_history'):
pdf.write(8,history)
pdf.output("bash.pdf")
最佳答案
.bash_history
文件按顺序包含整个历史记录。您可以简单地将文件加载到 python 列表中,然后根据您的要求拼接列表。
例如,以下代码片段将打印历史文件中的最后 20 条语句:
打印列表(open('{PATH}/.bash_history'))[-20:]
关于python - 使用 python 打印最近 20 个 bash 历史记录并保存在 pdf 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53810945/