提问人:Kikolo 提问时间:2/24/2023 最后编辑:Kikolo 更新时间:2/24/2023 访问量:56
避免由于 Web 服务器上的写入限制而创建临时文件
Avoid creation of a temporary file due to write restrictions on web servers
问:
我有一个使用两个独立包的代码(我们称它们为 packageA 和 packageB)。PackageA 有一个函数,可以将某些数据的“.pdf”写入磁盘。PackageB 包含一个名为 的方法,用于读取 pdf 并提取一些信息。工作流使我必须使用 packageA 函数编写一个临时文件,使用 packageB 函数读取它,然后删除它并继续使用 packageB 的不同工具。write(outputPath: str, ...)
read(inputPath: str, ...)
当我在本地机器上运行程序时,一切正常。但是,我需要在无法写入磁盘的外部服务器上运行此代码。我有哪些选择?我尝试使用包将文件写入内存,但我没有成功,因为 packageA 的函数需要一个字符串作为参数,它将在其中创建“pdf”。有什么帮助吗?tempfile
write
编辑:这是一个最小的例子,这两个包实际上是 和 .为了重现这个例子,除了这两个软件包之外,你还需要安装musescore,因为这些方法会对它进行内部调用(通过music21
PyMuPdf
write
os
)
import music21
import fitz
import os
piece = music21.corpus.parse('bwv66.6')
piece.write("musicxml.pdf", fp="temp.pdf")
with fitz.open("temp.pdf") as pdf:
# do some stuff with 'pdf'
pdf.save("final.pdf")
os.system("rm temp.pdf")
答: 暂无答案
评论
tempfile
是一个常规文件,无论是否支持内存。它应该有效。如果您使用的是上下文管理器方法,它可能会创建文件,然后将其删除,然后才能将其传递给 。你能发布你的代码吗?PackageB
tempfile
, fp= tempfile.gettempdir() + "/temp.pdf"
fitz.open(tempfile.gettempdir() + "/temp.pdf")...
tempfile.SpooledTemporaryFile
max_size
tempfile.gettempdir()