如何在Quarto中使用post-render删除文件?
解决Quarto post-render删除文件报错问题
问题出在del、rm是系统shell的内置命令,而非独立可执行程序,Quarto直接调用时会找不到对应的程序。解决方法是通过系统shell来执行这些命令,或者用跨平台脚本实现:
按操作系统配置
Windows系统
通过cmd /c调用内置命令,配置示例:
project: type: website post-render: cmd /c del *.pdf
如果需要执行多条命令:
post-render: - cmd /c del *.pdf - cmd /c del *.log
Linux/macOS系统
通过bash -c调用内置命令,配置示例:
project: type: website post-render: bash -c "rm *.pdf"
多条命令的写法:
post-render: - bash -c "rm *.pdf" - bash -c "rm *.log"
跨平台通用方案(推荐)
写一个Python脚本实现文件删除,适配所有操作系统:
- 创建
delete_pdfs.py脚本:
import os import glob # 删除当前目录下所有PDF文件 for file_path in glob.glob("*.pdf"): try: os.remove(file_path) print(f"Deleted: {file_path}") except Exception as e: print(f"Failed to delete {file_path}: {e}")
- 在Quarto配置中调用该脚本:
project: type: website post-render: python delete_pdfs.py
注意:如果PDF文件不在项目根目录,需要修改脚本中的路径(比如
glob.glob("docs/*.pdf")),确保路径正确。
内容的提问来源于stack exchange,提问作者Alberson Miranda
相关产品推荐
相关产品推荐

